JavaScript Reference ยท Function
Closures
Reference for JavaScript closures: how a function remembers variables from its outer scope, private state, loop pitfalls with var and let, and memory notes.
A closure is a function together with the variables it can see from the place where it was created. When an inner function uses a variable from an outer function, the engine keeps that variable alive for as long as the inner function can still be called, even after the outer function has returned.
Every function in JavaScript is a closure in this sense; the word usually describes the interesting case where the outer function is already finished but its variables are still in use.
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
How closures work
When a function is created, it records the scope it was created in. When it runs, it looks up names first in its own scope, then in that recorded outer scope, and so on up to the global scope. The recorded scope is a live link to the variables, not a copy of their values. If the outer variable changes, the closure sees the new value.
Each call of the outer function creates a fresh scope, so makeCounter() called twice gives two independent counters, as the first example shows. The variable count cannot be reached from outside except through the functions that closed over it, which makes closures the classic way to get private state.
Common uses
- Factories and configuration:
multiplier(3)returns a function withfactorfixed. - Private state: counters, caches and ID generators without exposing the variable.
- Callbacks and event handlers: a handler remembers the data that was in scope when you registered it.
- Partial application, memoization and debouncing: helpers that keep timers or caches between calls.
- Module pattern: an immediately invoked function returns an API, keeping everything else private (see IIFE).
Usage notes
varin loops shares one variable across all iterations, so every closure sees the final value.letcreates a new binding per iteration, which is why the second loop prints0, 1, 2.- Closures capture variables, not values. If you want the value at creation time, copy it into a new variable or pass it as an argument.
- Memory: captured variables stay alive as long as any closure that uses them is reachable. Long-lived closures that capture large objects keep them in memory; remove event listeners and set references to
nullwhen you are done. - Engines optimize: only variables that a closure really uses need to be kept, but you should not rely on this for memory-sensitive code.
- Classes and
#privatefields are the modern alternative for private state; closures are lighter for small cases. thisis not closed over in regular functions, only in arrow functions, which takethisfrom the surrounding scope.
Browser support
Closures are part of the core language and work everywhere. Per-iteration let bindings in loops are available in all current browsers.