JS: Array.prototype.keys

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2015)

xArray.keys()
  • Return a Generator.
  • Each entry is the index of the array.
console.log(JSON.stringify(Array.from(["a", "b", "c"].keys())) === `[0,1,2]`);
// true

Verify Result is a Generator

// verify the result is iterator and iterable

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

// is iterable
console.log(Reflect.has(xx, Symbol.iterator));
// true

// is iterator
console.log(Reflect.has(xx, "next"));
// true

Array Iterator