1for (var i = 0; i < 5; i++) {
2 setTimeout(function() {
3 console.log(i);
4 }, i * 1000);
5}
6// Expected: 0, 1, 2, 3, 4
7// Actual: 5, 5, 5, 5, 5
no lines flagged
#011PracticeEasy8 min · 50 XP
var Hoisting in Loop Closures
All timeout callbacks log the same value instead of their loop index. Find the scoping fault.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.