Open In App

JavaScript Array keys() Method

Last Updated : 18 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

javascript




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


Output:

0 1 2

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

javascript




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


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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads