In the global scope, there are some subtle differences:
Function declarations are "hoisted" to the "top" of its enclosing scope, they are evaluated and initialized before the actual execution of the code starts, meaning that if you wanted, you could even use them in a point before the function definition, for example:
console.log(typeof foo); // "function"
console.log(foo()); // "bar"
function foo () {
return "bar";
}
Note: If you try the above snippet in Firebug, it will show a different behavior, that's because the Mozilla implementations support functions as statements, and the Firebug evaluation is done internally, inside a try-catch
block, enclose the above code in a function and it will show the described behavior.
Assignments take place at run-time, therefore the value of foo
would be available only after this takes place.
Function declarations also are bound as non-deletable properties of the lexical enviroment where they are bound, in the global scope, declarations are bound as properties of the global object, which is the top-most object at the scope chain, for example:
function foo() {
//..
}
console.log(delete window.foo); // false
console.log(typeof foo); // "function"
window.bar = function() {
//..
}
console.log(delete window.bar); // true
console.log(typeof bar); // "undefined"