🎙 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 15 of 15

Which change makes for (var i = 0; i < 3; i++) setTimeout(() = console.log(i)) print 0, 1, 2?

Which change makes for (var i = 0; i < 3; i++) setTimeout(() => console.log(i)) print 0, 1, 2?

  1. A.Use const instead of var
  2. B.Use let instead of var, so each iteration gets its own i
  3. C.Increase the timeout
  4. D.Use an arrow function
Answer & explanation

Correct answer: Use let instead of var, so each iteration gets its own i

let creates a new binding per iteration; const would fail because i++ changes it.

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 →