How can the current number of i be accessed in a for of loop?

You can use the entries function. It will return a index/value pair for each entry in the array like:

[0, "one"]
[1, "two"]
[2, "three"]

Use this in tandem with array destructuring to resolve each entry to the appropriate variable name:

const arr = ["one", "two", "three"]
for(const [index, value] of arr.entries()) {
  console.log(index, value);
}

Babel REPL Example

Leave a Comment