Array.prototype.keys()

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

Syntax

keys()

Return value

A new Array iterator object.

Examples

Key iterator doesn't ignore holes

const arr = ['a', , 'c'];
const sparseKeys = Object.keys(arr);
const denseKeys = [...arr.keys()];
console.log(sparseKeys); // ['0', '2']
console.log(denseKeys);  // [0, 1, 2]

Specifications

Specification
ECMAScript Language Specification
# sec-array.prototype.keys

Browser compatibility

BCD tables only load in the browser

See also