Both definitions are function expressions, unlike function declarations or functions created by the Function constructor. Both assign a function to the alertMessage variable. The difference is that the first function is called, and the second is anonymous.
Named functions are commonly used in function declarations, such as
function alertMessage(message) { ... }
In this case, the function declaration creates a variable in the current scope, called alertMessage , which refers to this function. Function declarations go up to the top of the current area, so you can call declared functions before they are defined in your js file.
A named function used in a function expression (for example, the original question) does not create this variable or does not rise at the top of the execution area, therefore, by convention, most function expressions are anonymous. The only advantage of naming a function is that the variable name is associated with the function (although, as the CMS mentions, it depends on the implementation), and the function name is derived from the toString method. This can be useful during debugging (instead of having Firebug (?) Output for a huge list of anonymous function calls).
More information at MDC
alunny
source share