🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.setTimeout always prints 3
  2. B.All callbacks share one var i, which is 3 when they finally run
  3. C.var variables are constant
  4. 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 →