← Quiz: Scope, Closures, Hoisting and this: all 15 questions
Quiz: Scope, Closures, Hoisting and this · Question 5 of 15
Why does a for (var i = 0; i < 3; i++) loop with setTimeout print 3 three times?
Why does a for (var i = 0; i < 3; i++) loop with setTimeout print 3 three times?
- A.setTimeout always prints 3
- B.All callbacks share one var i, which is 3 when they finally run
- C.var variables are constant
- D.The loop runs three extra times
Answer & explanation
Correct answer: All callbacks share one var i, which is 3 when they finally run
The timers run after the loop ends; var gives one shared variable, while let gives one per iteration.
Can you get the rest right without peeking?
Take the timed Quiz: Scope, Closures, Hoisting and this quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →