Which fix removes the bug?
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}
- Declare the counter with
let iinstead - Declare the counter with
var iinstead - Move the timeout call outside the loop
- Pass
ias the third setTimeout argument
var is function-scoped so every callback sees the final 3; let binds a fresh i per iteration.
Play Problem Solving ▸