Open In App

JavaScript Array entries() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The entries() method in JavaScript is used to create an iterator that returns key/value pairs for each index in the array.

It allows iterating over arrays and accessing both the index and value of each element sequentially.

Array entries() Syntax

array.entries()

Array entries() Parameters

This method does not accept any parameters.

Array entries() Return value

Returns an array of indexes and values of the given array on which the Array.entries() method is going to work. 

JavaScript Array entries() Method Examples

Example 1: Iterating Array Entries in JavaScript

The code initializes an array languages and creates an iterator g using the entries() method. It then iterates over each key/value pair in the iterator using a for...of loop, logging each pair along with the string “geeks” to the console.

Javascript




let languages = ["HTML", "CSS", "JavaScript", "ReactJS"];
let g = languages.entries();
for (x of g) {
    console.log("geeks",x);
}


Output

geeks [ 0, 'HTML' ]
geeks [ 1, 'CSS' ]
geeks [ 2, 'JavaScript' ]
geeks [ 3, 'ReactJS' ]





Example 2: Iterating Over Array Entries in JavaScript

The code initializes an array fruits_names containing fruit names. It then creates an iterator fruits_array_iterator using the entries() method. Two calls to next().value are made on the iterator, logging the key/value pairs (index and corresponding element) to the console.

Javascript




let fruits_names = ['apple', 'banana', 'mango'];
let fruits_array_iterator = fruits_names.entries();
console.log(fruits_array_iterator.next().value);
console.log(fruits_array_iterator.next().value);


Output

[ 0, 'apple' ]
[ 1, 'banana' ]



We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers:

The browsers supported by the JavaScript Array entries() method are listed below:

  • Google Chrome 38.0
  • Microsoft Edge 12.0
  • Mozilla Firefox 28.0
  • Safari 8.0
  • Opera 25.0

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Last Updated : 11 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads