How come the below code creates closures on different values of i? As far as I know closure is when a function remembers its lexical scope even when that function is executed outside of its lexical scope. When a loop is declared, let creates a block scope variable called i. But that declaration happens once. From that logic, I would expect this loop to log 5 5 5 5 5, but instead it logs 0 1 2 3 4. If I declared new variable inside the loop let j = i and pass that into console.log(j), then I would understand the output being 0 1 2 3 4 because each iteration creates a new block scoped variable and function closes on that. But how does the following closes on different i variable when only one is supposed to be created for the whole block scope? What am I missing here?
for(let i = 0; i<5;i++){
setTimeout(function(){
console.log(i);
},i*1000);
}