Tuesday, 3 May 2022

Not able to understand the output of this for loop in JS

I have understood why the output of this code should be 3 3 3.

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 1);
}

I am not able to understand, though, why the output of this code is 0 1 2.

for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 1);
}

I want more clarity with the output of the second for loop.



from Not able to understand the output of this for loop in JS

No comments:

Post a Comment