Immediately Invoked Function Expressions (IIFE) Quiz Quiz

Challenge your understanding of Immediately Invoked Function Expressions (IIFEs) in JavaScript. This quiz focuses on identifying IIFE syntax, scope management, function expression behavior, and common pitfalls in writing and using IIFEs.

  1. Identifying an IIFE Syntax

    Which of the following code snippets correctly represents the syntax of an Immediately Invoked Function Expression in JavaScript?

    1. {function(){ /* code */ }}();
    2. function IIFE() { /* code */ }
    3. function(){ /* code */; }();
    4. (function(){ /* code */ })();

    Explanation: The correct IIFE syntax wraps the anonymous function inside parentheses, followed by another set of parentheses to invoke it, as in (function(){ /* code */ })();. The second option is missing the parentheses around the function definition, making it invalid. The third is a named function declaration, which is not invoked immediately. The fourth option introduces a block scope without properly declaring a function expression, which also makes it incorrect.

  2. Purpose of IIFEs

    When using an Immediately Invoked Function Expression, what is one main advantage related to variable scope?

    1. It makes all variables global by default.
    2. It creates a private scope for variables declared inside the function.
    3. It forces variables to be block-scoped only.
    4. It allows variables to persist after the function ends.

    Explanation: IIFEs create a private scope, restricting variables declared within to only be accessible inside the function, preventing them from polluting the global scope. They do not make variables global; that would be less secure and can cause naming collisions, making option two incorrect. Option three refers to block scope, but IIFEs use function scope, not block scope. Option four is incorrect because variables inside IIFEs are not persistent after execution.

  3. Return Value Usage

    What will be the value of the variable 'result' after executing this code snippet: var result = (function() { return 42; })();?

    1. 42
    2. null
    3. undefined
    4. function

    Explanation: The IIFE executes immediately and returns 42, so the variable result holds the value 42. It does not return undefined or null because a numeric value is explicitly returned. The option 'function' is incorrect because the result is the evaluated return value, not the function itself.

  4. IIFEs with Parameters

    Given the code snippet: (function(a, b) { return a + b; })(3, 5), what does this IIFE return?

    1. 35
    2. 3 + 5
    3. undefined
    4. 8

    Explanation: The IIFE takes two parameters, 3 and 5, and returns their sum, which is 8. '35' is incorrect because the values are not being concatenated as strings. 'undefined' is incorrect because the function clearly returns a value. '3 + 5' is just a string and not the evaluated sum.

  5. Naming and Invocation Mistakes

    Which of the following statements about IIFEs is accurate regarding function naming and immediate execution?

    1. An IIFE is the same as a function declaration.
    2. IIFEs require that the function be declared globally.
    3. Only named function expressions can be used for IIFEs.
    4. An IIFE can be an anonymous function or a named function expression, as long as it is invoked immediately.

    Explanation: IIFEs can use anonymous or named function expressions as long as they are invoked directly after declaration. They do not require global declaration; in fact, they often avoid it. Both anonymous and named expressions are valid, so limiting to just named is incorrect. An IIFE is not the same as a function declaration, which does not get invoked immediately and is not wrapped in parentheses.