Solved

When to use a function or a var

Posted on 2015-02-05
6
65 Views
Last Modified: 2015-02-10
Hi,

In JavaScript when would you use a var over a function or vice versa?
Or is it just personal preference?

I.e

var investment = function(){};
as opposed to
function investment() {};
0
Comment
Question by:Soluga
6 Comments
 
LVL 11

Expert Comment

by:Dany Balian
Comment Utility
Well it has to do with reusability! If u want to use the function al over again then use functions! If it's a one time thing then just type it!
0
 
LVL 1

Author Comment

by:Soluga
Comment Utility
Hi Danny,

So would you then use a var to create an instance of the function?
Such as ..

var = investment();

function investement(){};
0
 
LVL 32

Assisted Solution

by:Big Monty
Big Monty earned 250 total points
Comment Utility
if you expect a value to be returned from the function, then it would be better to put it a variable. if not, then just calling the function by name would be the best way to go (purely for readability purposes)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Accepted Solution

by:
Dany Balian earned 250 total points
Comment Utility
if you need to use the var many times.. then yes you do that..
instead of doing

if (getproductdiscount()>0) {
total = total + getproductdiscount();
} else {
total = total - getproductdiscount();
}
in this case getproductdiscount() will be executed and calculated 3 times

however
var disc=getproductdiscount();
if (disc>0) {
total = total + disc;
} else {
total = total - disc;
}
will run the function just once
0
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
I think this explains the difference between function declarations and function expressions : https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/
0
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
It's interesting that you accepted those two comments even though they answer a completely different question.
What they answer is why put the result of a function call in a variable
result = myfunction();
if (result > 0)
    total = total + result;

Open in new window

vs. using directly the result of the function.
if (myfunction() > 0)
    total = total + myfunction();

Open in new window

Your question was about the difference between a function declaration
function investment() {}

Open in new window

and a function expression
var investment = function(){};

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now