Array.prototype.entries()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Theentries()
method ofArray
instances returns a newarray iterator object that contains the key/value pairs for each index in the array.
Try it
const array1 = ["a", "b", "c"];const iterator1 = array1.entries();console.log(iterator1.next().value);// Expected output: Array [0, "a"]console.log(iterator1.next().value);// Expected output: Array [1, "b"]
Syntax
js
entries()
Parameters
None.
Return value
A newiterable iterator object.
Description
When used onsparse arrays, theentries()
method iterates empty slots as if they have the valueundefined
.
Theentries()
method isgeneric. It only expects thethis
value to have alength
property and integer-keyed properties.
Examples
Iterating with index and element
js
const a = ["a", "b", "c"];for (const [index, element] of a.entries()) { console.log(index, element);}// 0 'a'// 1 'b'// 2 'c'
Using a for...of loop
js
const array = ["a", "b", "c"];const arrayEntries = array.entries();for (const element of arrayEntries) { console.log(element);}// [0, 'a']// [1, 'b']// [2, 'c']
Iterating sparse arrays
entries()
will visit empty slots as if they areundefined
.
js
for (const element of [, "a"].entries()) { console.log(element);}// [0, undefined]// [1, 'a']
Calling entries() on non-array objects
Theentries()
method reads thelength
property ofthis
and then accesses each property whose key is a nonnegative integer less thanlength
.
js
const arrayLike = { length: 3, 0: "a", 1: "b", 2: "c", 3: "d", // ignored by entries() since length is 3};for (const entry of Array.prototype.entries.call(arrayLike)) { console.log(entry);}// [ 0, 'a' ]// [ 1, 'b' ]// [ 2, 'c' ]
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.entries |