JavaScript Array entries() Method

Below is the example of the Array entries() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
        <body style="text-align: center;">
            <h1 style="color: green;">
              GeeksforGeeks
            </h1>
            <b>
              JavaScript | Array entries() Method
            </b>
            <p id="geeks"></p>
      
            <script>
                var lnaguages = ["HTML", "CSS", 
                                 "JavaScript", "ReactJS"];
                var g = lnaguages.entries();
      
                for (x of g) {
                    document.getElementById("geeks")
                      .innerHTML += x + "<br>";
                }
            </script>
        </body>
    </html>

    chevron_right

    
    

  • Output:

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



More example codes for the above method are as follows:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

0Geeks1for2Geeks

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

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2