I am trying to understand the reason but didn't found it yet. So asking here.
The below prints i value fine when I use let i = 0
for(let i=0; i < 5; i++){
setTimeout(() => {
console.log(i) // 0 1 2 3 4
}, 1000)
}
The below prints i value 5 when I use var i = 0
for(var i=0; i < 5; i++){
setTimeout(() => {
console.log(i) // 5 5 5 5 5
}, 1000)
}
Please help me to understand why it works this way?.