JS: Array.prototype.keys

By Xah Lee. Date: . Last updated: .

New in JS2015.

myArray.keys()

Return a Iterator (also is a Iterable Object), each entry is the index of the array.

Array.from(["a", "b", "c"].keys(), (x) => {
  console.log(x);
});
/*
0
1
2
*/

Example

This example shows the result is iterator and iterable:

const xx = ["a", "b", "c"].keys();

// is a iterator
console.log(Object.prototype.toString.call(xx) === "[object Array Iterator]");

// is also a iterable
console.log(Symbol.iterator in xx);

JS Array.prototype Properties