const fns = [];
for (var i = 0; i < 3; i++) {
fns.push(() => console.log(i));
}
fns.forEach(f => f());const fns = [];
for (let i = 0; i < 3; i++) {
fns.push(() => console.log(i));
}
fns.forEach(f => f());Bug: var is function-scoped, not block-scoped. All three closures share the same i. By the time they run, i is already 3.
Explanation: let creates a new binding per loop iteration. Each closure captures its own i — 0, 1, 2 — instead of sharing the single var i.
Key Insight: var in loops + closures = all closures see the final value. Always use let when creating closures inside loops.