Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Set.prototype.keys() — a set value iterator

Set.prototype.keys() returns a new iterator over a set's values in insertion order. It returns neither indexes nor an array: in a Set, keys and values refer to the same elements.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

set.keys()

set.keys() requires no arguments and returns a new iterator over the set's values in insertion order. A Set has no separate keys or indexes. In the unmodified standard library, Set.prototype.keys and Set.prototype.values reference the same function; the name keys provides consistency with the Map interface.

The iterator is not an array. Calling next() advances it by one element, while [...iterator] collects its remaining elements into an array. Once exhausted, next() returns a result with done: true. Calling set.keys() again creates an independent iterator.

The iterator is not a snapshot: an element deleted before it is visited is skipped, and an element added before iteration finishes may be visited. Calling the method with a this value that lacks the internal Set data throws TypeError.

In the example, the repeated "red" does not create a second element. The first next() retrieves "red", so spreading the iterator contains only "blue".

Examples

const colors = new Set(["red", "blue", "red"]);
const iterator = colors.keys();

console.log(Set.prototype.keys === Set.prototype.values);
console.log(iterator.next().value);
console.log([...iterator].join(","));
console.log(iterator.next().done);

Result

true
red
blue
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources