Closure
- Pronunciation
- KLOH-zher
In short
A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.
What is a closure?
A closure is created when a function is defined inside another function and uses variables from that outer function. The inner function keeps a reference to those variables, not a copy, so it can still read and update them later, even after the outer function has finished. In JavaScript, every function forms a closure over the scope it was defined in.
Closures work because of lexical scoping, which means a function's access to variables is decided by where it is written in the code, not by where it is called from. When an outer function finishes, its variables would normally be thrown away, but if an inner function still refers to them, they stay alive in memory.
A backpack is a helpful analogy for a closure: when a function is created, it packs up the variables around it and carries them wherever it goes. Closures are used for private state (data that outside code cannot reach directly), function factories, event handlers, and callbacks that need to remember context.
A closure is often confused with a callback. A callback is simply a function passed to another function to be called later, while a closure is about a function remembering its surrounding variables. Many callbacks are also closures, but the two terms describe different ideas.
At a glance
Key takeaways
- A closure is a function plus the variables it captured from its surrounding scope.
- Captured variables stay alive as long as the closure exists.
- Closures capture variables by reference, so they always see the latest value.
- They enable private state, function factories, and callbacks that remember context.
Example
// makeCounter returns a function that "closes over" count
function makeCounter() {
let count = 0; // private: not reachable from outside
return function () {
count += 1; // the inner function still has access
return count;
};
}
const counter = makeCounter();
counter(); // 1
counter(); // 2 (count was remembered between calls)Readers ask
Why are closures useful?
Closures let a function keep private state between calls without using global variables. They are the foundation of patterns like counters, memoization (caching results), and event handlers that remember which data they belong to.
What is the difference between a closure and a callback?
A callback is a function you pass to another function so it can be called later. A closure is a function that remembers variables from where it was created; a callback is often a closure, but the terms describe different things.
Can closures cause memory leaks?
They can if a long-lived closure, such as an event listener that is never removed, keeps references to large objects that are no longer needed. Removing unused listeners and avoiding unnecessary captured data prevents this.
See also
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- CallbackProgramming Fundamentals, p. 7A callback is a function passed as an argument to another function, which then calls it later, for example when a task finishes or an event happens.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- ScopeProgramming Fundamentals, p. 50Scope is the region of a program where a name, such as a variable or function, is visible and can be used, which decides what each line of code can reach.
Sources
Spotted a mistake or something missing on this page?Suggest an edit