If I run this code then it console 4 at 5 time, can anyone explain why it is happening ?
let number = 1;
for (var i=0; i<5; i++){
number=i;
setTimeout(()=>{
console.log(number)
},1000)
}
Also if I modify this to as below then output is 5 times 5 ?
for (var i=0; i<5; i++){
setTimeout(()=>{
console.log(i)
},1000)
}
And why it prints 0 to 4 when we use let like
for (let i=0; i<5; i++){
setTimeout(()=>{
console.log(i)
},1000)
}