Javascript - Array keys() Method

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

Description

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

Syntax

array.keys()

Parameter Values

No parameters.

Return

An Array Iterator object

Example

Create an Array Iterator object, with keys for each item in the array:

Demo

var myArray = ["XML", "Json", "Database", "Mango"];
var x = myArray.keys();
console.log( x.next().value);//from w  ww. j  a  va 2s .c  om
console.log( x.next().value);
console.log( x.next().value);
console.log( x.next().value);

//Array keys() method returns a new Array Iterator that 
//contains the keys for each index in the array: 

const breakfast = ['XMLs', 'Screens', 'Keyboards']; 
const kBreakfast = breakfast.keys(); 

console.log(kBreakfast.next().value); // 0 
console.log(kBreakfast.next().value); // 1 
console.log(kBreakfast.next().value); // 2

Result