Array.prototype.keys()
The keys()
method returns a new Array
Iterator object that contains the keys for each index in the array.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
arr.keys()
Return value
A new Array
iterator object.
Examples
Key iterator doesn't ignore holes
var arr = ['a', , 'c'];
var sparseKeys = Object.keys(arr);
var denseKeys = [...arr.keys()];
console.log(sparseKeys); // ['0', '2']
console.log(denseKeys); // [0, 1, 2]
Specifications
Browser compatibility
BCD tables only load in the browser