+ 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); }

8th Sep 2019, 7:42 PM
Amirabbas
Amirabbas - avatar
2 Answers
+ 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.
8th Sep 2019, 7:49 PM
KfirWe
KfirWe - avatar