I'm aware of the difference between the var & let variable declaration. I just executed the same piece of code using for loop & while loop. To my surprisingly I got the different output & I'm not able to differentiate the execution behavior difference between them.
I have attached 2 code snippets using While loop & For loop.
If anyone is aware of this then please help me.
let i = 0;
while(i < 5) {
setTimeout(function() {
console.log(i)
}, 1000);
i++;
}
for(let j = 0; j < 5; j++) {
setTimeout(function() {
console.log(j)
}, 1000);
}
Thanks!