Closure
A function bundled with the variables from its enclosing scope at the point it was created.
Closure is a function value that carries a reference to the variables it used from its enclosing scope, so those variables stay alive and visible to the function even after the scope that declared them has otherwise finished executing. function counter() { let n = 0; return () => ++n; } returns a closure: the returned arrow function keeps n alive across calls, private to that one instance, with no other way to reach it.
The mechanism is lexical scoping resolved at creation time, not call time: the closure captures the variable, not a copy of its value at that moment, which is why the classic loop-variable bug exists — a closure created inside a for (var i ...) loop in JavaScript captures the single shared i, so all the closures see whatever i ended up being after the loop ended, not the value at each iteration. let (block-scoped) fixes this by creating a fresh binding per iteration; understanding why is the fastest way to actually understand closures rather than just use them.
Closures are the usual way to fake private state in languages without a private keyword: the counter example above has no way for outside code to read or set n directly, only through the function that closed over it. This is the same trick module patterns in older JavaScript relied on before ES modules existed.
The cost is that a closure keeps its captured variables alive for as long as the closure itself is reachable, which can pin down more memory than expected if a closure retains a large object it only needed briefly — a common source of memory leaks in long-lived event handlers or caches built out of closures rather than an explicit data structure.
See also5
Higher-Order Function
A function that takes another function as an argument, returns one, or both.
Computation & Algorithms7 connections
Currying
Transforming a function of several arguments into a chain of one-argument functions, each returning the next.
Computation & Algorithms4 connections
Pure Function
A function whose output depends only on its input and which causes no observable effect outside itself.
Computation & Algorithms13 connections
Lazy Evaluation
Deferring a computation until its result is actually needed, and then caching it so it never runs twice.
Computation & Algorithms10 connections
JavaScript
The programming language of the web browser, written in ten days in 1995, standardized as ECMAScript, and now run everywhere from servers to build tools despite quirks it can never remove.
Web Platform10 connections
Linked from9
- Call StackComputation & Algorithms
The region of memory where each function call keeps its local variables and the address to return to, and the source of stack traces, stack overflows, and a classic class of security holes.
- CurryingComputation & Algorithms
Transforming a function of several arguments into a chain of one-argument functions, each returning the next.
- Garbage CollectionComputation & Algorithms
A runtime automatically reclaiming memory occupied by objects nothing can reach anymore.
- Higher-Order FunctionComputation & Algorithms
A function that takes another function as an argument, returns one, or both.
- ImmutabilityComputation & Algorithms
A value that cannot change after construction, so sharing it never risks a caller seeing another caller's edit.
- JavaScriptWeb Platform
The programming language of the web browser, written in ten days in 1995, standardized as ECMAScript, and now run everywhere from servers to build tools despite quirks it can never remove.
- Lazy EvaluationComputation & Algorithms
Deferring a computation until its result is actually needed, and then caching it so it never runs twice.
- Pure FunctionComputation & Algorithms
A function whose output depends only on its input and which causes no observable effect outside itself.
- Tail Call OptimizationComputation & Algorithms
Reusing a function's stack frame for a call in tail position so recursion runs in constant stack space.