What is a closure?

In short
A closure is a function that keeps access to the variables from the place where it was created, even after that place has finished running. In JavaScript, every function forms a closure. This lets a function remember data and use it later, without passing that data in each time.
The idea of lexical scope
Before looking at closures, it helps to know how a language decides which variable a name refers to. This rule is called lexical scoping. It means the position of a variable in the source code decides where that variable can be used.
Consider a function that defines another function inside it. The inner function can read the variables of the outer function. The outer function cannot see variables that only live inside the inner one. The rule follows the code layout, not the order in which functions happen to be called.
Returning a function that remembers
Normally you might expect a function's local variables to disappear once the function returns. In JavaScript they do not, if an inner function still refers to them. The inner function is returned, and it carries its surrounding variables with it. That combination of a function and its surrounding variables is the closure.
The documentation describes it this way: a closure is the function together with the environment where it was declared. The environment holds the variables that were in scope at the moment the function was created. Each time an outer function runs, it creates a new environment, so each returned function gets its own copy of those variables.
A simple example
Here is a small function factory. It takes a number and returns a function that adds that number to whatever it receives.
function makeAdder(x) {
return function (y) {
return x + y;
};
}
const add5 = makeAdder(5);
const add10 = makeAdder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12
Both functions share the same code, but they remember different values of x. The value 5 belongs to add5, and the value 10 belongs to add10. You can think of each one as a small function with its own saved setting.
Where closures are useful
Closures let you attach data to a function. This is similar to an object that holds properties and methods together. In practice, you can often use a closure where you would otherwise write a small object with one method.
In front-end code this comes up often. A click handler is a callback, which is a function that runs when an event happens. A closure can carry settings into that callback. For example, a factory can create one handler per button, and each handler knows the size it should set. The handler does not need to receive that value again when the button is clicked.
Private data with closures
JavaScript did not have private class members for a long time. Closures offered a way to hide data. A variable declared inside a function is not reachable from outside, so only the functions you return can touch it.
function makeCounter() {
let privateCounter = 0;
return {
increment() { privateCounter += 1; },
value() { return privateCounter; },
};
}
const counter = makeCounter();
counter.increment();
console.log(counter.value()); // 1
The variable privateCounter cannot be read or changed directly. Each counter returned by makeCounter has its own hidden variable, so two counters do not affect each other.
Common mistakes
A frequent source of bugs comes from var. Because blocks do not create their own scope for var, a variable declared with var inside a loop is shared by every closure created in that loop. Using let or const gives each loop iteration its own block-scoped variable, which avoids this problem.
Another issue is memory. A closure keeps its surrounding variables alive as long as the closure itself is alive. If you hold on to many closures that reference large data, that data stays in memory. Keep closures only as long as you need them.
Alternatives
A class or a plain object with methods can do the same job as a closure. Classes are often easier to read when many methods share state, while closures are short and suit small pieces of behavior. Choose the form that makes the code clearest for your team.
FAQ
Is every function in JavaScript a closure?
Technically yes, since every function forms a closure with the environment where it was declared. The term is most useful when a function is used outside the place where it was created, for example when it is returned or passed as a callback.
Do closures copy the values of outer variables?
No. A closure keeps a reference to the variable itself, not a copy of its value. If the outer variable changes later, the closure sees the new value. Each call to the outer function creates a fresh set of variables.
Why do closures in a loop sometimes show the same value?
This happens when the loop variable is declared with var, because var is function-scoped and all closures share one variable. Declaring the variable with let or const gives each iteration its own block-scoped variable.
Do closures make code slower?
They add a small amount of memory use, because the captured variables stay alive. For most code this cost is negligible. It only matters when you create very large numbers of closures that hold big data.
Sources
This page is based on the official documentation below. Read it for full detail.
Closure — official documentation ↗