Do you know what will be the output of this code?
JavaScript
function counter() {
let count = 0;
return () => ++count;
}
const inc = counter();
inc();
inc();
console.log(inc());
Show answer and explanation
The answer is 3.
The arrow function is a closure: it keeps a reference to count even after counter() has returned. Every call increments the same variable.