Array.prototype.keys()
Returns a new array iterator that yields the index of each element.
Syntax
arr.keys() Returns
Array Iterator — An iterator yielding the keys (indices) of the array.
Examples
console.log([...['a', 'b', 'c'].keys()]);
Output
[ 0, 1, 2 ]
for (const i of ['x', 'y'].keys()) console.log(i);
Output
0
1
Notes
Unlike `Object.keys`, this includes indices for empty slots in sparse arrays.
Use `values` for elements and `entries` for index/value pairs.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 38 |
| firefox | 28 |
| safari | 8 |
| edge | 12 |
| node | 0.12 |