EasyClosures & Scope💻 Output Question

Classic var in loop with setTimeout

💡

Hint

var in loops creates one shared binding. Every closure captures the same variable, not its value at that iteration.

What does this output?

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}

Correct Output

3
3
3

Why this output?

Explanation: var is function-scoped — all three callbacks close over the same i. By the time they run, the loop has finished and i is 3.

Key Insight: var in loops creates one shared binding. Every closure captures the same variable, not its value at that iteration.

More Closures & Scope Output Questions

Easylet in loop — each iteration gets own bindingMediumIIFE captures loop variableMediumClosure mutation persists across callsEasylet block scope — inner does not leak

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz