DEV Community

Discussion on: 5 things that might surprise a JavaScript beginner/ OO Developer

Collapse
 
maowtm profile image
maowtm • Edited

You should probably also mention that

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

gets you 1 2 3, whereas

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

will output 4 4 4. IMO this is one of the major reason why scope of loop variable matters.

Some comments have been hidden by the post's author - find out more