Joined August 2013
·

Jon Tascher

Greater New York City Area
·
·
·

There are actually a couple of other ways to define a function in javascript.

This is identical to your second example (eg, you cannot call it until after it has been defined in the code.) I am just including it here for the sake of completeness:

var foo = function foo() {
    console.log('bar');
}

This version is the same as the above, however, the function name is different from the variable name. The important thing to note about this declaration (and the above) is that the function name only works from within the function. If there is another function named "bar" elsewhere in the code, it will not be overridden.

var foo = function bar() {
    console.log('bar');
}

This syntax will execute the function immediately, though since it is anonymous you cannot reuse this function later in your code:

(function() {
    console.log('bar');
)();

There is a lot of great information on this topic here: http://kangax.github.io/nfe/

Achievements
12 Karma
0 Total ProTip Views