Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Map.prototype.keys: key iterator

The keys method returns a live iterator over Map keys in insertion order without copying their associated values.

Version: ECMAScript 2015 onward; example target: Node.js 24.22.2

Syntax

map.keys()

map.keys() creates a new Map iterator. Each successful next() returns the next key in insertion order; object keys are the same references, not clones. The iterator does not mutate the Map, and separate calls to keys() maintain independent positions.

The iterator is lazy, not a snapshot. It skips an entry deleted before its visit and can observe an entry added before reaching the end. Updating the value of an existing key creates no new position. After done: true has been returned once, later additions do not resume that exhausted iterator. A -0 key is stored and returned as +0.

The method takes no meaningful arguments and requires a receiver with Map internal data; a plain object, Set, or unhandled Proxy throws TypeError. It has been standard since ECMAScript 2015. Use entries() or default Map iteration when values are also needed.

Examples

const objectKey = {};
const map = new Map([[objectKey, 1], ["b", 2]]);
const keys = map.keys();
console.log(keys.next().value === objectKey);
map.delete("b");
map.set("c", 3);
const second = keys.next();
console.log(second.value, second.done);
console.log(keys.next().done);
map.set("d", 4);
console.log(keys.next().done);
const zeroKey = new Map([[-0, "zero"]]).keys().next().value;
console.log(Object.is(zeroKey, 0));
try { Map.prototype.keys.call({}); } catch (error) { console.log(error instanceof TypeError); }

Result

true
c false
true
true
true
true

Sources