JavaScript Array keys() Method

JavaScript Array keys() Method creates and returns a new iterator object which holds the key for every index in the array.

The keys() method does not change the original array.

The keys() method does not ignore empty array elements.

Syntax

array.keys()

Parameters

It does not hold any parameter.

Return Value

It returns a new array iterator object.

Examples

const array1 = ['a', 'b', 'c'];
const iterator = array1.keys();

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

// expected output: 0
// expected output: 1
// expected output: 2
const languages = ["JavaScript", "Java", , "C++", "Python"];
let iterator = languages.keys();

for (let key of iterator) {
  console.log(key);
}
/*
expected output: 
0
1
2
3
4*/