Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Functions  ›  Lesson

IIFEs

IIFEs 10 minAn Immediately Invoked Function Expression wraps code in a function and calls it on the spot with (), creating a private, locked scope — anything declared inside is unreachable from outside, which is how JavaScript faked private state before modules existed.

IIFEs

In short: an Immediately Invoked Function Expression wraps code in (function () { ... }) and calls it on the spot with a trailing () — the parentheses around the function turn it into an expression, and the second pair calls it immediately, once. Anything declared inside — let, const, even other functions — lives in a scope that's locked from the outside the moment the IIFE finishes running. This was how JavaScript faked private state before real modules existed, and it still shows up in scripts that want to avoid leaking names into the global scope.

(function () { let secret = 42; })(); runs immediately because of the trailing ( ) and creates a locked scope where secret lives — never reachable from outside, so console.log(secret) outside throws a ReferenceError

A value that never escapes

The simplest IIFE: a value that exists only for the moment the function runs, then disappears.

javascript

Inside the IIFE, secret is perfectly normal. The moment the function finishes, that scope is gone — secret was never declared anywhere reachable from outside, so asking for it throws a ReferenceError, the same as asking for a name that never existed.

A counter with genuinely private state

The useful pattern: return an object from the IIFE, so callers get controlled access without ever touching the private variable directly.

javascript

count lives inside the IIFE's locked scope — there's no counter.count to read or overwrite by accident. increment and value were defined inside that same scope, so they keep access to count through closure, even after the IIFE itself has already finished running.

An arrow function works too

Any function expression can be immediately invoked, including an arrow:

javascript

The shape is the same — wrap it so it's an expression, then call it right there. Arrow IIFEs are common in modern code for a quick one-off scope with less punctuation than the classic function version.

Quick practice

quick drill

What makes (function () { ... })() run immediately, instead of just being defined?

quick drill

After (function () { let x = 5; })(); runs, what does console.log(x) do outside it?

quick drill

Why does an IIFE-returned object's method still work after the IIFE itself has finished running?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA private counter

Explain how const counter = (function () { let count = 0; return { increment() { count++; return count; } }; })(); keeps count private, while still letting code call counter.increment() from outside.