I know let is scoped to the nearest enclosing block. but still dont understand why below given code is output so differently.
for (let i = 0; i < 5; i++) {
setTimeout(function() {
console.log(i)
}, 1000)
}
//0,1,2,3,4
for (var i = 0; i < 5; i++) {
setTimeout(function() {
console.log(i)
}, 1000)
}
//5,5,5,5,5