Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Set.prototype.values() — a set's value iterator

The values() method returns a new iterator over a Set's values in insertion order. It does not create an array or copy the set; reading advances the iterator, and changes to the set can affect the remaining traversal.

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.values()

Call Set.prototype.values() as set.values(), without arguments. It returns a new iterator over values in insertion order, not an array. The call itself does not change the set. Adding an existing value again neither creates a duplicate nor changes its position.

The iterator's next() method returns an object with value and done properties. Once exhausted, these are undefined and true, respectively; further calls do not restart traversal. A new traversal requires another call to set.values(). The iterator supports for...of and [...iterator], which consumes its remaining elements.

The iterator is not a snapshot: it skips values deleted before they are visited and can visit values added before traversal finishes. Deleting and re-adding a value places it at the end, so it can be visited again if its earlier occurrence was already read. Avoid uncontrolled additions during iteration.

By default, Set.prototype.keys and Set.prototype[Symbol.iterator] reference the same function as Set.prototype.values. In contrast, entries() yields [value, value] pairs. values() requires a receiver with internal Set data; calling it on a plain object or array throws TypeError.

Examples

const set = new Set(["a", "b", "a"]);
const iterator = set.values();

console.log(JSON.stringify(iterator.next()));
console.log(JSON.stringify([...iterator]));
console.log(JSON.stringify(iterator.next()));
console.log(JSON.stringify([...set.values()]));

Result

{"value":"a","done":false}
["b"]
{"done":true}
["a","b"]

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