This was posted originally on Stack overflow meant for answering a question about JavaScript Function Declaration and Function Expression. Now I post it here as an official recored.
Function Declaration
function foo() { ... }
Because of function hoisting, the function declared this way can be called both after and before the definition.
Function Expression
- Named function expression
var foo = function bar() { ... }
var foo = function() { ... }
foo() can be called only after creation.
In order to invoke a function immediately, please take a look at: Immediately-Invoked Function Expression (IIFE), and why it's not called self-executing anonymous function.
(function() { ... }());
Conclusion?
Crockford recommends to use function expression because it makes it clear that foo is a variable containing a function value. Well, personally, I prefer to use Declaration unless there is a reason for Expression.