26

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.

0

1 Answer 1

21

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)

Sign up to request clarification or add additional context in comments.

3 Comments

FYI, looks like the author got rid of the comments on that post. Still worth a read though.
var foo = function(){} is just VARIABLE DEFINING, before defining will be undefined.function foo(){} is regular function declaration, if before declare use it,the JavaScript interpreter will find it even if they aren't declared until further parts in your code.
@Eric Andres, Seems like the author got rid of the website/domain too ;)