JavaScript
JavaScript Closures, Finally Explained With One Example
By: Md Tarek · 20 Oct 2026
A closure is a function that remembers the variables from the place where it was created — even after that place has finished running.
The one example
function counter() {
let count = 0;
return () => ++count;
}
const next = counter();
next(); // 1
next(); // 2count should be gone after counter() returns. It is not, because the returned arrow function closes over it.
Why I care
- Every React hook relies on this.
- Every event handler that reads stale state is this bug in disguise.
- If a value feels "frozen" in a callback, suspect a closure first.
