JavaScript · Middle · Question 12 of 75
What does this code print?
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}- ✓Correct answer:
3 3 3 - 2
0 1 2 - 3
undefinedthree times
Explanation
var creates one variable for the whole function, and by the time the timers fire, the loop has finished with i = 3. With let, each iteration gets a new binding and the output is 0 1 2.