Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Array.prototype.entries

Returns index–element pairs for a for...of loop.

Available since: ES2015

Syntax

array.entries()

Return value

IteratorAn iterator of [index, element] pairs.

Examples

The index and the value in a for...of loop:

const fruits = ['apple', 'pear'];
 
for (const [index, fruit] of fruits.entries()) {
  console.log(index, fruit);
}

Output:

0 apple
1 pear

Notes

It is an iterator, not an array: loop over it with for...of or turn it into an array with Array.from() or spread.

See also

  • for...of — Loops over the values of arrays, strings, Maps, Sets and other iterables.
  • Array.prototype.keys — Returns the indexes of an array.
  • Object.entries — Returns an array of property name and value pairs.

Related recipes

In the tutorial

  • Iterating over arrays — How to go through every element of an array in JavaScript: the for loop, for...of, forEach and entries.

Thanks for reading!

Donate to the site — we work hard for you.