JavaScript Array keys() Method


The keys() method creates and returns a new iterator object which holds the key for every index in the array. This method does not affect the original array.

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

The keys() method returns an Array Iterator object with the keys of an array.

Syntax

array.keys()

Example

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var fk = fruits.keys();

for (x of fk) {
  document.getElementById("demo").innerHTML += x + "<br>";
}

Example -