Loop callbacks and captured bindings
Predict both lines and the number of bindings each loop creates for the callbacks.
Code Snippet
const first = [], second = [];
for (var i = 1; i <= 3; i++) first.push(() => i * 2);
for (let j = 1; j <= 3; j++) second.push(() => j * 2);
console.log(first.map(fn => fn()).join(" "));
console.log(second.map(fn => fn()).join(" "));Expected Output & Explanation
8 8 8
2 4 6 The first group shares i, which is 4 after its loop. The let loop gives each callback a different per-iteration binding.
Module Context & References
JavaScript · Language output drills Trace closures, receivers, prototypes, coercion and array edge cases
Personal Notes · Markdown