Skip to content
Related Articles

Related Articles

JavaScript Array entries() Method

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 07 Nov, 2022
Improve Article
Save Article

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 Array.entries() method is going to work. 

Below examples illustrate the JavaScript Array entries() Method

Example: 

html




<p id="geeks"></p>
<script>
    var languages = ["HTML", "CSS", "JavaScript", "ReactJS"];
    var g = languages.entries();
    for (x of g) {
        document.getElementById("geeks").innerHTML += x + "<br>";
    }
</script>


Output:

Example: 

JavaScript




<script>
    var arr = ["HTML", "CSS", "JS",
                 "Bootstrap", "PHP"];
    var seqn = arr.entries();
    console.log("Applying the Array entries method:"
                + "<br>");
     
    for (var x of seqn) {
        console.log(x);
    }
</script>


Output:

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

Program 2: 

JavaScript




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


Output:

0Geeks1for2Geeks

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

Javascript




<script>
    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);
</script>


Output:

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

Supported Browsers: The browsers supported by 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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!