Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between assigning function to variable or not

I have a worked on a couple different projects and I have seen two different ways of creating jQuery/JavaScript functions.

The first:

function testFunction(){

};

The second:

var testFunction = function (){

};

Is there a difference between these?

like image 342
dan_vitch Avatar asked Jun 21 '12 21:06

dan_vitch


People also ask

What is the difference between a function and a variable?

Remember that variables are items which can assume different values. A function tries to explain one variable in terms of another.

Why is a function assigned to a variable?

You can use them in 'maps' to give functions to values $map = array(1 => function() { return "first one";}); echo $map[1](); . Declaring a function to a variable allows you to change the flow of execution without changing your code by simply reassigning that variable to a different function.

Can I assign function to a variable?

In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Simply assign a function to the desired variable but without () i.e. just with the name of the function.

What is the difference between a variable and a function in JavaScript?

A variable is something, which stores data. A function is a bunch of code, which can be executed, if you call. But a function can be a variable, too, as it stores data and values, too. See the following syntax: var functionname = function(){} .


1 Answers

The main difference is the first one (a function declaration) is hoisted to the top of the scope in which it is declared, whereas the second one (a function expression) is not.

This is the reason you are able to call a function that has been declared after you call it:

testFunction();
function testFunction() {}

You can't do that with a function expression, since the assignment happens in-place:

testFunction();
var testFunction = function() {}; //TypeError

There is also a third form (a named function expression):

var testFunction = function myFunc() {};

In this case, the identifier myFunc is only in scope inside the function, whereas testFunction is available in whatever scope it is declared. BUT (and there's always a but when it comes to Internet Explorer) in IE below version 9 the myFunc identifier wrongly leaks out to the containing scope. Named function expressions are useful when you need to refer to the calling function (since arguments.callee is deprecated).


Also note that the same is true for variable declarations:

console.log(x); //undefined (not TypeError)
var x = 10;

You can imagine that the JavaScript engine interprets the code like this:

var x; //Declaration is hoisted to top of scope, value is `undefined`
console.log(x);
x = 10; //Assignment happens where you expect it to
like image 153
James Allardice Avatar answered Oct 16 '22 23:10

James Allardice