Following code only prints 5
for(var x=0; x<5; x++) {
setTimeout(()=>console.log(x), 0)
}
And following code prints 0,1,2,3,4
for(let x=0; x<5; x++) {
setTimeout(()=>console.log(x), 0)
}
What way setTimeout is behaving? Second argument to setTimeout is 0, so it should immediately execute the callback, which is the first argument. If the second argument were some positive number, say 10, then I understand by the callback is called, x would have reached its final value and that also I think should have been 4 in place of 5.