Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Array keys() Method

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 16 Dec, 2022
Improve Article
Save Article

The Javascript array.keys() method is used to return a new array iterator which contains the keys for each index in the given input array. Syntax:

array.keys()

Parameters: This method does not accept any parameters. 

Return Values: It returns a new array iterator. 

The below example illustrates the Array keys() method in JavaScript:

Example: In this example, we will see the use of Array.keys() method.

javascript




<script>
    // Taking input as an array A
    // containing some elements.
    var A = [ 5, 6, 10 ];
      
    // array.keys() method is called
    var iterator = A.keys();
      
    // printing index array using the iterator
    for (let key of iterator) {
        console.log(key);
    }
</script>

Output:

0 1 2

Example: In this example, we will see the use of Array.keys() method.

javascript




<script>
    // Taking input as an array A
    // containing some elements.
    var A = [ 'gfg', 'geeks', 'cse', 'geekpro' ];
      
    // array.keys() method is called
    var iterator = A.keys();
      
    // printing index array using the iterator
    for (let key of iterator) {
        console.log(key);
    }
</script>

Output:

0 1 2 3

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 keys() 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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!