A closure is a function that remembers and can access variables from the scope it was created in, even after that outer function has finished running.
Closures happen naturally whenever a function is defined inside another function and references variables from that outer function.
function makeCounter() {
let count = 0;
return function() {
count++;
return count;
};
}
const counter = makeCounter();
counter(); // 1
counter(); // 2
counter(); // 3
Even though makeCounter() has already finished executing, the returned inner function still has access to — and remembers — the count variable from its original scope. Each call to makeCounter() creates a fresh, independent closure with its own count.
Closures are the basis for data privacy in JavaScript (variables like count above can't be accessed directly from outside), and they're used constantly in callbacks, event handlers, and functional programming patterns throughout modern JavaScript.
Last reviewed: September 2026