Please find my snippet here,
for (var i=0;i<11;i++) {
setTimeout( () => console.log(i), 10);
}
How it print 11 for 11 times? since i was set < 11?
if i console it without function it print 1-10 .
for (var i=0;i<11;i++) {
setTimeout( console.log(i), 10);
}
this gives me 1-10. i am wonder how its getting changed if i include function without condition?
i, and when the timers fire its value is 11.setTimeoutis an asynchronous function, so by the time the first log happens, the for loop has already finished running (it performsi++untili<11is false, and the first time that happens is wheni === 11)