Problem Statement
What best describes a closure in JavaScript?
Explanation
A closure occurs when an inner function remembers and can access variables from its outer function’s scope, even after the outer function has finished executing.
Closures are used for data privacy, function factories, and maintaining state across calls.
Typical use cases include counters, once-only initializers, and event handlers.
Code Solution
SolutionRead Only
function makeCounter() {
let count = 0;
return () => ++count;
}
const inc = makeCounter();
console.log(inc()); // 1
console.log(inc()); // 2Practice Sets
This question appears in the following practice sets:
