6 6 6 6 6
Why: there are 3 nested scopes. The anonymous function scope doesn't have access to i, nor does setTimeout, but global scope does—after it's exited the for loop.
Fix: IIFE (Immediately Invoked Function Expression)
for (var i=1; i<=5; i++) {
(function(i){
setTimeout(function(){
console.log("i: " + i);
}, i*1000);
})(i);
} // 1 2 3 4 5
Or, use let, which is blocked scoped:
for (let i=1; i<=5; i++) {
setTimeout(function(){
console.log("i: " + i);
}, i*1000);
}
// 1 2 3 4 5