The Wayback Machine - https://web.archive.org/web/20240204103841/https://www.geeksforgeeks.org/javascript-array-entries-method/
Open In App
Related Articles

JavaScript Array entries() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

This article will cover all the facts in detail which are related to the method, namely Array.entries() under Arrays in JavaScript.

The Array.entries() method works on iterable objects such as an array (or any data iterable data structure) and it is used to fetch all the entries of the same data structure. This method is used to get a new Array that contains the key and value pairs for each index of an array. 

Syntax:

array.entries()

Parameters: This method does not accept any parameter. 

Return value: It returns an array of indexes and values of the given array on which the Array.entries() method is going to work. 

The below examples illustrate the JavaScript Array entries() Method

Example 1: In this example, we will see the basic use of the Javascript Array.entries() method.

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: In this example, we will see the use of the Javascript Array.entries() method.

JavaScript




let arr = ["HTML", "CSS", "JS",
           "Bootstrap", "PHP"];
let seqn = arr.entries();
console.log("Applying the Array entries method:");
 
for (let x of seqn) {
    console.log(x);
};


Output:

Applying the Array entries method:
0,HTML
1,CSS
2,JS
3,Bootstrap
4,PHP

Example 3: 

JavaScript




const x = ["Geeks", "for", "Geeks"];
 
for (const [index, element] of x.entries())
    console.log(index, element);


Output:

0 'Geeks'
1 'for'
2 'Geeks'

Example 4: In this example, we will understand the use of the above-illustrated method in a somehow different way using several other methods which is next().

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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 30 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials