+ 1
For loop in js
Can someone explain why are these two different for (var i = 1; i < 5; i++){ setTimeout(() => { console.log(i); }, 1000); } for (let i = 1; i < 5; i++){ setTimeout(() => { console.log(i); }, 1000); }
2 odpowiedzi
+ 3
Unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope, let allows you to declare variables that are limited in scope to the block, statement, or expression in which they are used.