JavaScript .entries()

ldraupp's avatar
Published Aug 6, 2025
Contribute to Docs

The entries() method returns a new array iterator object that contains key/value pairs for each index in the array. This is useful when both the index and value of array elements are needed during iteration.

Syntax

array.entries()

Parameters:

The entries() method does not take any parameters.

Return value:

A new Array Iterator object containing [index, value] pairs

Example

In this example the .entries() method is used to iterate over an array of cat names, accessing both the index and value in each loop:

const cats = ['Peche', 'Moana', 'Pintassilga'];
const iterator = cats.entries();
for (let [index, name] of iterator) {
console.log(`Cat #${index}: ${name}`);
}

The output of this code is:

Cat #0: Peche
Cat #1: Moana
Cat #2: Pintassilga

Codebyte Example

In this example the loop prints each index and corresponding fruit name by iterating through the key/value pairs returned by .entries():

Code
Output
Loading...

All contributors

Contribute to Docs