← All posts
postApril 19, 2026
Closure — Functions That Remember
#javascript#closures#scope

javascript
function makeCounter(start = 0) {
let count = start; // private to this call
return {
increment() { return ++count; },
decrement() { return --count; },
value() { return count; }
};
}
const a = makeCounter(10);
const b = makeCounter(0);
a.increment(); // 11
a.increment(); // 12
b.increment(); // 1 — independent state