Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'var foo = function ...' and 'function foo() ...' [duplicate]

Possible Duplicates:
“Usual” functions vs function variables in JavaScript
What do you call this JavaScript syntax, so I can research it?

Is there a fundamental difference between

function foo()
{
    things();
}

and

var foo = function()
{
    things();
}

Or is function ... just syntactical sugar?

Thanks in advance.


1 Answers

They are different (but produce similar results). Basically, the first is an actual named function. The second is a regular variable declaration with an anonymous function attached to it. There are some subtle differences...they are summed up nicely here:

JavaScript Function Declaration Ambiguity (Be sure to read the comments too...more good info there)

like image 90
Justin Niessner Avatar answered Sep 12 '25 10:09

Justin Niessner