JavaScript: Array.prototype.keys

By Xah Lee. Date: . Last updated: .

New in JS2015.

arrayX.keys()
Return a Iterator (also is a Iterable Object), each entry is the index of the array. That is, usually 0, 1, 2, etc.
for (const e of ["a", "b", "c"].keys()) {
 console.log( e );
}

// prints
// 0
// 1
// 2

A range function:

// a range function
console.log( 
 [... (Array(9)).keys()]
);
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]

This example shows the result is iterator and iterable:

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

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

// is also a iterable
console.log( 
    Symbol.iterator in et
); // true
BUY ΣJS JavaScript in Depth