Consider these 2 snippets:
With let
:
for(let i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i);
}, 100)
}
With var
:
for(var i = 0; i < 10; i++) { //HERE
setTimeout(function() {
console.log(i);
}, 100)
}
From my understanding, let
only affects the scoping of the variables, so why are the outputs of the 2 loops should be same, so why does let
print 0-9 and var
prints 10 10 times?