I am copying an excerpt from Javascript post. Need to know what exactly author is trying to convey.
In loops, you get a fresh binding for each iteration if you let-declare a variable. The loops that allow you to do so are: for, for-in and for-of.
This looks as follows:
let arr = []; for (let i=0; i < 3; i++) { arr.push(() => i); } console.log(arr.map(x => x())); // [0,1,2]
In contrast, a var declaration leads to a single binding for the whole loop (a const declaration works the same):
let arr = []; for (var i=0; i < 3; i++) { arr.push(() => i); } console.log(arr.map(x => x())); // [3,3,3]
Getting a fresh binding for each iteration may seem strange at first, but it is very useful whenever you use loops to create functions (e.g. callbacks for event handling) that refer to loop variables.