A closure captures the variables of its defining scope, keeping them alive after the outer function returns. That powers private state, memoisation and counters.
function nonce(start = 0) {
let n = start;
return () => n++;
}Beware capturing a loop variable declared with `var` — every closure sees the same binding; `let` gives one per iteration.