JavaScript · Junior · Question 18 of 75
What does this code print?
function makeCounter() {
let count = 0;
return () => ++count;
}
const c = makeCounter();
c(); c();
console.log(c());- ✓Correct answer:
3 - 2
1 - 3
0
Explanation
This is a closure: the returned function remembers the count variable from the scope where it was created, and each call increments it. Calling makeCounter() again would create an independent counter.