🟒 EasyClosures & ScopeπŸ’» Output Question

Classic var in loop closure

πŸ’‘

Hint

var leaks out of block scope. All closures share one variable.

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, not block-scoped. All 3 callbacks close over the SAME i variable. By the time they run, the loop has finished and i = 3.

Key Insight: var leaks out of block scope. All closures share one variable.

More Closures & Scope Output Questions

🟒 Easylet in loop closure (fix)β†’πŸŸ‘ MediumClosure counterβ†’πŸŸ‘ MediumIIFE closureβ†’πŸ”΄ HardClosure variable lookupβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz