Skip to the solution
MediumFrontend6 min read

What is an IIFE?

882 views

The problem

Question presented to candidate: "What is an IIFE, why would you use one, and does a variable declared inside one leak out to the surrounding scope?"

What a strong answer should cover:

  • πŸ“Œ Interview term: IIFE (Immediately Invoked Function Expression) β€” a function that is defined and called in the same statement, typically written as (function () { ... })(), running its body immediately without needing a separate, later invocation elsewhere in the code.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: a var declared inside an IIFE's body genuinely does not leak into the surrounding scope, because the IIFE's function body creates its own, private scope, exactly like any other function call.
  • πŸ“Œ Interview term: the classic real motivation (pre-ES6) β€” before let/const's block scoping existed, an IIFE was the standard way to create a private, isolated scope β€” avoiding polluting the global scope with helper variables, and (verified directly) fixing the classic var-in-loop-closure bug by wrapping each iteration's body in its own immediately-invoked function, correctly capturing each loop value.
  • A precise answer names that an IIFE can be written with an arrow function too ((() => { ... })()), and that the leading parenthesis around the function expression is required specifically to tell the parser it is an expression, not a function declaration (which cannot be immediately invoked the same way).
  • A precise answer names that IIFEs are genuinely less common today β€” ES modules already provide their own private, file-level scope, and let/const block scoping covers most of the "avoid leaking a helper variable" use case IIFEs used to be needed for β€” but understanding the pattern remains relevant for reading legacy code and library-bundling output.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly answering whether a variable leaks (with real proof) is the strong signal.

Code / implementation expected: Yes β€” a real IIFE demonstrating both its immediate execution and its private-scope guarantee is the clearest, most convincing proof.

  • iife

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: an IIFE runs immediately, does not leak its variables, and fixes the classic var-in-loop-closure bug

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 62 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How does Array.reduce work?

Continue