DEV Community

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

Collapse
 
softchris profile image
Chris Noring

hey Will.. I've seen many articles referring it as if, for, while, doesn't create a new scope but function does, here for example scotch.io/tutorials/understanding-.... I mean I totally see your way as well, especially with MDN saying this about let, let allows you to declare variables that are limited to the scope of a block statement. Yea I'll see if I can amend this in a good way, appreciate the comment :)

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