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.

Community
  • 1
  • 1

1 Answers1

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)

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 2
    FYI, looks like the author got rid of the comments on that post. Still worth a read though. – Eric Andres May 08 '13 at 15:02
  • 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. – 王奕然 Aug 23 '13 at 13:08
  • @Eric Andres, Seems like the author got rid of the website/domain too ;) – Sachem Oct 16 '17 at 16:09