For example:

    While this technique “works”, it is not necessarily very ideal. There are a few problems it introduces. The first is that we have to declare a named-function , which means that the identifier name foo itself “pollutes” the enclosing scope (global, in this case). We also have to explicitly call the function by name (foo()) so that the wrapped code actually executes.

    It would be more ideal if the function didn’t need a name (or, rather, the name didn’t pollute the enclosing scope), and if the function could automatically be executed.

    Fortunately, JavaScript offers a solution to both problems.

    1. var a = 2;
    2. (function foo(){ // <-- insert this
    3. var a = 3;
    4. console.log( a ); // 3
    5. })(); // <-- and this
    6. console.log( a ); // 2

    Let’s break down what’s happening here.

    First, notice that the wrapping function statement starts with (function... as opposed to just function.... While this may seem like a minor detail, it’s actually a major change. Instead of treating the function as a standard declaration, the function is treated as a function-expression.

    Note: The easiest way to distinguish declaration vs. expression is the position of the word “function” in the statement (not just a line, but a distinct statement). If “function” is the very first thing in the statement, then it’s a function declaration. Otherwise, it’s a function expression.

    The key difference we can observe here between a function declaration and a function expression relates to where its name is bound as an identifier.

    In other words, (function foo(){ .. }) as an expression means the identifier foo is found only in the scope where the .. indicates, not in the outer scope. Hiding the name foo inside itself means it does not pollute the enclosing scope unnecessarily.

    You are probably most familiar with function expressions as callback parameters, such as:

    1. setTimeout( function(){
    2. console.log("I waited 1 second!");
    3. }, 1000 );

    This is called an “anonymous function expression”, because function()... has no name identifier on it. Function expressions can be anonymous, but function declarations cannot omit the name — that would be illegal JS grammar.

    Anonymous function expressions are quick and easy to type, and many libraries and tools tend to encourage this idiomatic style of code. However, they have several draw-backs to consider:

    1. Anonymous functions have no useful name to display in stack traces, which can make debugging more difficult.

    2. Without a name, if the function needs to refer to itself, for recursion, etc., the deprecated arguments.callee reference is unfortunately required. Another example of needing to self-reference is when an event handler function wants to unbind itself after it fires.

    3. Anonymous functions omit a name that is often helpful in providing more readable/understandable code. A descriptive name helps self-document the code in question.

    Inline function expressions are powerful and useful — the question of anonymous vs. named doesn’t detract from that. Providing a name for your function expression quite effectively addresses all these draw-backs, but has no tangible downsides. The best practice is to always name your function expressions:

    Invoking Function Expressions Immediately

    1. var a = 2;
    2. var a = 3;
    3. console.log( a ); // 3
    4. })();
    5. console.log( a ); // 2

    This pattern is so common, a few years ago the community agreed on a term for it: IIFE, which stands for Immediately Invoked Function Expression.

    Of course, IIFE’s don’t need names, necessarily — the most common form of IIFE is to use an anonymous function expression. While certainly less common, naming an IIFE has all the aforementioned benefits over anonymous function expressions, so it’s a good practice to adopt.

    1. var a = 2;
    2. (function IIFE(){
    3. var a = 3;
    4. console.log( a ); // 3
    5. })();
    6. console.log( a ); // 2

    There’s a slight variation on the traditional IIFE form, which some prefer: (function(){ .. }()). Look closely to see the difference. In the first form, the function expression is wrapped in ( ), and then the invoking () pair is on the outside right after it. In the second form, the invoking () pair is moved to the inside of the outer ( ) wrapping pair.

    These two forms are identical in functionality. It’s purely a stylistic choice which you prefer.

    Another variation on IIFE’s which is quite common is to use the fact that they are, in fact, just function calls, and pass in argument(s).

    For instance:

    We pass in the window object reference, but we name the parameter , so that we have a clear stylistic delineation for global vs. non-global references. Of course, you can pass in anything from an enclosing scope you want, and you can name the parameter(s) anything that suits you. This is mostly just stylistic choice.

    Another application of this pattern addresses the (minor niche) concern that the default undefined identifier might have its value incorrectly overwritten, causing unexpected results. By naming a parameter undefined, but not passing any value for that argument, we can guarantee that the undefined identifier is in fact the undefined value in a block of code:

    1. undefined = true; // setting a land-mine for other code! avoid!
    2. (function IIFE( undefined ){
    3. var a;
    4. if (a === undefined) {
    5. console.log( "Undefined is safe here!" );
    6. }
    7. })();
    1. var a = 2;
    2. (function IIFE( def ){
    3. def( window );
    4. })(function def( global ){
    5. var a = 3;
    6. console.log( a ); // 3
    7. console.log( global.a ); // 2
    8. });

    The def function expression is defined in the second-half of the snippet, and then passed as a parameter (also called def) to the IIFE function defined in the first half of the snippet. Finally, the parameter def (the function) is invoked, passing in as the global parameter.