A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-19.js
1constfns=[];
2for(leti=0;i<3;i++){fns.push(()=>i);}
3console.log(fns.map(f=>f()).join(","));
Scope
Answer & explanation
Console output
0,1,2
Why
Unlike var, a let in a for-header gets a fresh binding for every iteration of the loop, and the previous value is copied into the next iteration. Each closure therefore captures a distinct i holding 0, 1, and 2 respectively. This per-iteration binding is exactly why let fixes the infamous loop-closure bug.