JS Built-ins
Variablesintermediate

Variables β€” let in loop closures (per-iteration binding)

let in loops: each iteration gets a fresh binding

When you use let in a for loop, JavaScript creates a new binding for each iteration. Closures created inside the loop each capture their own copy of i.

Challenge: Write letLoopClosure() that:

  1. Creates an array fns
  2. Uses a for (let i = 0; i < 3; i++) loop and pushes () => i to fns
  3. Returns fns[0]() β€” this returns 0 (the iteration-specific binding)
πŸ’‘ Hints (1)
  • β€’ let creates a new scope per iteration β€” each closure captures its own i