Skip to main content

JavaScript IIFE — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript IIFE lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Write an IIFE

    easy10 min

    Use an IIFE to work out 6 times 7 and store the answer.

  2. 2

    Pass an argument

    easy10 min

    Pass the name Ada into an IIFE and return a greeting.

  3. 3

    Keep a variable private

    easy10 min

    Show that a variable inside an IIFE cannot be seen outside.

  4. 4

    Return an object

    easy10 min

    Return a config object from an IIFE and show its version.

  5. 5

    An arrow IIFE

    medium10 min

    Write the same idea as an arrow IIFE returning the word ready.

Find the bug(5)

  1. 1

    Fix the IIFE that never runs

    easy8 min

    Without the final brackets the function is stored, not called.

  2. 2

    Fix the argument in the wrong brackets

    easy8 min

    The argument belongs in the calling brackets.

  3. 3

    Fix the leaked variable

    easy8 min

    The variable is declared outside, so it is not private.

  4. 4

    Fix the missing return

    easy8 min

    The IIFE builds the object but never gives it back.

  5. 5

    Fix the arrow IIFE syntax

    easy8 min

    The arrow function is never invoked.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace