Start here.
Functions can be written inside other functions. The inner function can read names declared in the outer function because those names are in its surrounding scope. A closure is the combination of a function and that continuing access. This is why a returned function can still use a local variable from a call that has already finished. The variable remains available for as long as a reachable function needs it.
In makeCounter, the outer call creates let count = 0 and returns an arrow function. The arrow is a short function expression: () => ++count means 'take no arguments, increase count, and return the new value'. If next stores the returned function, next() produces 1, then next() again produces 2. The outer call has ended, but the inner function still reaches the same count binding.
Each call of makeCounter creates its own count. Two returned counters do not share state unless they were made to reference the same outer binding. A closure does not freeze the original value; it keeps access to a changing binding. This idea becomes useful later for callbacks, event handlers, and private state. You can understand it by tracing which call created each name and which function still has access to it.
Build the model.
When a function is created, it keeps access to its surrounding lexical bindings. That relationship is a closure. The bindings are not copied into a frozen snapshot; the function can observe later changes to a binding that still exists.
Closures let you keep private state and create specialized functions. They also explain why event handlers and timers can use values from an earlier call. When a loop creates several handlers, ask whether they share one binding or each capture a different block-scoped binding.
How it works.
A function is created inside a scope, and its body can refer to names in that scope. Returning the function does not erase those bindings while they are still needed. In makeCounter, count belongs to one call of makeCounter. The returned arrow function can read and update that particular count later. This is how a function can carry state without exposing a public property.
The important detail is that closure captures a binding, not just the value at creation time. If the binding changes, a later call sees the new value. Two separate calls to makeCounter create two separate count bindings. That separation is useful for independent counters, timers, or event handlers. It can also surprise you if several callbacks accidentally share one outer let variable.
Read it line by line.
function makeCounter() {
let count = 0;
return () => ++count;
}
const next = makeCounter();First notice. The returned function can still reach count after makeCounter has returned.
Calling makeCounter creates count = 0 and returns the inner function; next now holds that function. Calling next() evaluates ++count, which changes the captured count to 1 and returns 1. Calling next() again changes the same binding to 2. The outer call has finished, but its count remains reachable through next.
See the rule again.
function counter() {
let n = 0;
return () => ++n;
}
const a = counter();
const b = counter();
console.log(a(), a(), b(), a());The values are 1, 2, 1, 3. All calls to a share the binding created by its counter() call. b has a different binding and starts at zero. This is a practical way to create isolated state without a global variable.
One thing to remember.
A closure remembers access to bindings, not a photograph of their values.