
Closures: private state, tiny surface
JavaScript closures give functions memory: private state without globals, safer APIs, and predictable behavior. See what they are, when to use them, and why.
Martin Ferret
September 8, 2025
A closure is a function that keeps access to the variables from the place it was created, even after that outer function has finished. It’s the simplest way to give code “memory” without classes or globals.
Below is a small, practical example. We’ll keep the function as-is, then walk through exactly what happens line by line and why this pattern is useful in real projects.
function createCounter(initialCount = 0) {
let count = initialCount;
return function next() {
count++;
return count;
};
}
const getNextCount = createCounter(10);
console.log(getNextCount()); // 11
console.log(getNextCount()); // 12
console.log(getNextCount()); // 13
How this works (step by step)
- You call
createCounter(10). Inside that call, JavaScript creates a new lexical environment wherecountis set to10. createCounterreturns the inner functionnext. Crucially,nextis closed over the environment wherecountlives. Returning the function does not discard that environment; it stays alive becausenextstill references it.- You assign the returned function to
getNextCount. Now, each call togetNextCount()runsnext(), which reads and updates the same hiddencountvariable:11,12,13, and so on. - Code outside cannot access
countdirectly (countis not a property ongetNextCount), which means the state is private by construction. When nothing referencesgetNextCountanymore, the closure (and its environment) can be garbage-collected.
That’s a closure in action: function + remembered context.
Why this pattern is valuable
- It gives you private state without globals or classes. The only way to change the state is through the function you returned, which makes behavior predictable and hard to misuse.
- It enforces a tiny API surface. Callers get
getNextCount()and nothing else; internals remain hidden. - It’s composable. You can create many independent counters (
createCounter(0),createCounter(100)) with zero risk of them interfering. - It’s testable and local. State lives right next to the logic that uses it.
When to reach for a closure
Use a closure whenever logic must remember something across calls but you don’t want to expose that state:
- Counters and toggles that persist between interactions.
- “Run once” initializers (avoid double-booting analytics, sockets, or widgets).
- Lightweight caches or debounced handlers that keep a timer or last result.
- Event handlers that need to carry an ID, token, or configuration without polluting the global scope.
A few practical notes
Closures capture references to variables, not copies. Avoid capturing large objects you don’t need, and clean up listeners or timers that close over data when you’re done.
Ready to prove your JavaScript skills?
Get certified in JavaScriptMore JavaScript articles
Keep learning JavaScript. Then get certified when you are ready.

Node 26 Is LTS: Your Date Bugs Just Lost Their Excuse
Node 26 LTS brings flag-free Temporal and new JavaScript APIs, offering safer date handling while requiring checks for removed APIs and the Linux libatomic dependency.
Martin Ferret

DOM Events & Modern Patterns Cheat Sheet
Learn modern DOM event handling, including propagation, delegation, listener cleanup, custom events, forms, state management, efficient mutations, and observers to build responsive, performant pages without leaks.
Martin Ferret

this in JavaScript: Binding Rules and Arrow Functions
JavaScript’s this depends on the call site, with explicit binding and new taking precedence; arrow functions inherit this, making them useful for callbacks but unsuitable as object methods.
Martin Ferret