JavaScript Array keys()

The JavaScript Array keys() method returns a new Array Iterator object that contains the keys for each index in the array.

The syntax of the keys() method is:

arr.keys()

Here, arr is an array.


keys() Parameters

The keys() method does not have any parameters.


Return value from keys()

  • Returns a new Array iterator object.

Notes:

  • The keys() method does not change the original array.
  • The keys() method does not ignore empty array elements.

Example: Using keys() method

const languages = ["JavaScript", "Java", , "C++", "Python"];
let iterator = languages.keys();

for (let key of iterator) {
  console.log(key);
}

Output

0
1
2
3
4

Note that the third empty element is not ignored.


Recommended Reading: JavaScript Array values()