There are some things you can do with a function expression that you can't with a declaration.
it could be immediately invoked, and the return value stored in the variable
if you're not in the global namespace, you could exclude the var
keyword to create a global
EDIT:
Here's an example of an immediate invocation. It returns a function to the myFunctionName
variable that has access to the variables and parameter scoped in the immediately invoked function.
var myFunctionName = function( v ) {
// do something with "v" and some scoped variables
// return a function that has access to the scoped variables
return function() {
// this function does something with the scoped variables
};
}( 'someVal' );
// now this function is the only one that has access
// to the variables that were scoped in the outer expression.
myFunctionName();
Here's an example where a function maintains a numeric value. You can repeatedly call the function, giving it a number to add to the count. It will always reference the current value, so each call is cumulative.
Example: http://jsfiddle.net/5uuLa/1/
var counter = function( value ) {
return function( addValue ) {
value += ~~addValue;
return value;
};
}( 10 ); // initialize with a value
// each call builds on the result of the previous
console.log( counter( 2 ) ); // 12
console.log( counter( 5 ) ); // 17
console.log( counter( 3 ) ); // 20
console.log( counter( 7 ) ); // 27
console.log( counter( 1 ) ); // 28