Dev HelpersOpen library
← Back

Preserve each iteration binding

Preserve each iteration binding in JavaScript.

Open this example in the interactive library

Source code

const callbacks = [];
for (let index = 0; index < 3; index++) {
  callbacks.push(() => index);
}
console.log(callbacks.map((read) => read()));

Sample output

[ 0, 1, 2 ]