Array.prototype.entries()

Returns a new array iterator that yields [index, value] pairs.

Since ES2015 Spec ↗

Syntax

arr.entries()

Returns

Array Iterator — An iterator yielding [index, value] pairs.

Examples

for (const [i, v] of ['a', 'b'].entries()) console.log(i, v);
Output
0 a
1 b
console.log([...['x'].entries()]);
Output
[ [ 0, 'x' ] ]

Notes

Handy with destructuring in `for...of` loops when you need both the index and the value. Includes empty slots in sparse arrays as undefined.

Browser & runtime support

EnvironmentSince version
chrome 38
firefox 28
safari 8
edge 12
node 0.12

See also