Speaking about the global context, both, the var
statement and a FunctionDeclaration
at the end will create a non-deleteable property on the global object, but the value of both can be overwritten.
The subtle difference between the two ways is that when the Variable Instantiation process runs (before the actual code execution) all identifiers declared with var
will be initialized with undefined
, and the ones used by the FunctionDeclaration
's will be available since that moment, for example:
alert(typeof foo); // 'function', it's already available
alert(typeof bar); // 'undefined'
function foo () {}
var bar = function () {};
alert(typeof bar); // 'function'
The assignment of the bar
FunctionExpression
takes place until runtime.
A global property created by a FunctionDeclaration
can be overwritten without any problems just like a variable value, e.g.:
function test () {}
test = null;
Another obvious difference between your two examples is that the first function doesn't have a name, but the second has it, which can be really useful when debugging (i.e. inspecting a call stack).
About your edited first example (foo = function() { alert('hello!'); };
), it is an undeclared assignment, I would highly encourage you to always use the var
keyword.
With an assignment, without the var
statement, if the referenced identifier is not found in the scope chain, it will become a deleteable property of the global object.
Also, undeclared assignments throw a ReferenceError
on ECMAScript 5 under Strict Mode.
A must read:
Note: This answer has been merged from another question, in which the major doubt and misconception from the OP was that identifiers declared with a FunctionDeclaration
, couldn't be overwritten which is not the case.