← 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?
- A.Use const instead of var
- B.Use let instead of var, so each iteration gets its own i
- C.Increase the timeout
- 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 →