When to use a function or a var

Posted on 2015-02-05
Last Modified: 2015-02-10

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


var investment = function(){};
as opposed to
function investment() {};
Question by:Soluga
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 11

Expert Comment

by:Dany Balian
ID: 40590685
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!

Author Comment

ID: 40590868
Hi Danny,

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

var = investment();

function investement(){};
LVL 33

Assisted Solution

by:Big Monty
Big Monty earned 250 total points
ID: 40591414
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)
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 11

Accepted Solution

Dany Balian earned 250 total points
ID: 40593032
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

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

Expert Comment

ID: 40593043
I think this explains the difference between function declarations and function expressions :
LVL 11

Expert Comment

ID: 40600192
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


Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The most up-to-date version of this article is on my Blog
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

623 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