Multicolor crayons

Javascript for of index

Starting with ECMA 6 we have a new loop iterating statement in the form of for…of. This helps us to loop thru an iterable object (array, string, map, set…) in a simpler fashion than the classic for loop.

To achieve this, we are going to use the entries() method of the Array:

for (const [index, element] of myArray.entries()) {
  console.log(index, element)
}

Leave a Reply

Your email address will not be published. Required fields are marked *