The first is the named function operator, the second assigns the expression of the anonymous function to the variable.
The function operator is added to the scope immediately - you do not need to run it before it can be called, so this works:
var y = sum(1, 2); function sum(x, y) { return x + y; }
But the function expression is assigned to the variable only when the code is executed, so this does not work:
The advantage of the expression form is that you can use it to assign different functions to the expression at different points - so you can change the function or use another in different conditions (for example, depending on the browser you are using).
The advantage of a named function statement is that debuggers can display that name. Although you can name function expressions:
var sum = function sum(x, y) { return x + y; }
But this can be confusing, as the two names are actually in different areas and relate to different things.
Daniel James Sep 22 '08 at 12:32 2008-09-22 12:32
source share