Function Closures
A closure keeps access to bindings from the scope where a function was created. Each call to this factory gets its own counter.
Where to run this
This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.
A practical example
function makeCounter() {
let count = 0;
return () => ++count;
}
const next = makeCounter();
console.log(next(), next());
What happens
The same closure produces 1 then 2.