Javascript - What is the output: let variables declarations in for loops?


What is the output of the following code?

i is declared using let:

let arr = []; 

for (let i=0; i < 3; i++) { 
    arr.push(function () { return i }); 

let value = arr[0](); 

console.log(value); /

Click to view the answer



When we use let in a for-loop, each iteration of the loop will get its own i variable and any closures created close over their own value of i.