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
let A = [5, 6, 10];
let iterator = A.keys();
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
let A = [ 'gfg' , 'geeks' , 'cse' , 'geekpro' ];
let iterator = A.keys();
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.
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!
Last Updated :
18 May, 2023
Like Article
Save Article