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:
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:
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.
This syntax will execute the function immediately, though since it is anonymous you cannot reuse this function later in your code:
There is a lot of great information on this topic here: http://kangax.github.io/nfe/