The difference between let
and var
is related to the lexical scoping and variable hoisting. But I have never seen how the values of variables change when passed as a parameter.
Example below:
for(let i =1; i<=5; i++){
setTimeout(
()=>{ console.log(i) },
i*1000)
}
prints the output [1,2,3,4,5] every second.
But when using var
for(var i =1; i<=5; i++){
setTimeout(
()=>{ console.log(i) },
i*1000)
}
prints [6, 6, 6, 6, 6] every second.