Functions are data
Functions in JavaScript are actually data. This is an important concept that we'll need later on. This means that you can create a function and assign it to a variable:
var f = function () { return 1; };
This way of defining a function is sometimes referred to as function literal notation.
The part function () { return 1; }
is a
function expression. A function expression can optionally have a name, in which case it becomes a named function expression
(NFE). So, this is also allowed, although rarely seen in practice (and causes IE to mistakenly create two variables in the enclosing scope: f
and myFunc
):
var f = function myFunc() { return 1; };
As you can see, there's no difference between a named function expression and a function declaration. But they are, in fact, different. The only way to distinguish between the two is to look at the context in which they are used. Function declarations may only appear in program code (in a body of another function or in...