CKTIS / JavaScript
Array.prototype.keys: an index iterator
keys returns an iterator of consecutive indexes from zero, including indexes that are holes.
Version: ECMAScript 2026 draft; example target: Node.js 24.22.2
Syntax
array.keys()array.keys() returns a new iterator of indexes 0, 1, …. It neither reads values nor tests whether an indexed property exists, so a sparse hole receives an ordinary index. The iterator is single-use, is itself iterable, and does not mutate the receiver.
Current length is checked at each next() rather than frozen when the iterator is created. An element appended before completion can therefore add an index, while shortening the array can finish iteration earlier. The method is generic for array-like receivers, but null/undefined throws TypeError. The example shows indexes of an empty-slot array and an index appended during iteration.
Examples
console.log([...new Array(3).keys()].join(","));
const values = ["A"];
const iterator = values.keys();
console.log(iterator.next().value);
values.push("B");
const appended = iterator.next();
console.log(appended.value, appended.done);Result
0,1,2 0 1 false