A closure is the combination of a function bundled together with references to its surrounding lexical state, allowing inner functions to retain variable access.
js
function makeNonce(start = 0) {
let n = start;
return () => n++;
}100 XP to Apprentice
Lexical environment and captured variables.
AI translates the explanation and quiz text while preserving code and answer scoring.
A closure is the combination of a function bundled together with references to its surrounding lexical state, allowing inner functions to retain variable access.
js
function makeNonce(start = 0) {
let n = start;
return () => n++;
}1. What does a closure capture?
2. Can closures cause memory leaks if retained indefinitely?
3. Why did var in for loops fail with closures?