I tried running this snippet of code for which I tried consoling the i
value inside setTimeOut()
function and the o/p i
got is 5 for 5 times with a time interval of 3 seconds
each, can anyone please explain this snippet?
for (var i = 0; i < 5; i++) {
console.log(i);
setTimeout(function() {
/////Why is this console.log printing 5?
console.log(i);
},
i * 3000);
console.log("i:", i)
}
OutPut:
0
i: 0
1
i: 1
2
i: 2
3
i: 3
4
i: 4
5
5
5
5
5