CKTIS / JavaScript
Set.prototype.forEach — running a callback for set values
Set.prototype.forEach calls a function for set values in insertion order. It passes the value twice and the set itself, and the method returns undefined.
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.forEach(callback[, thisArg])
callback(value, valueAgain, set)Set.prototype.forEach calls callback for set values in insertion order. It returns undefined; it does not create a new set and ignores values returned by the callback.
The callback arguments are (value, valueAgain, set): the first two contain the same value, and the third is the set being traversed. The second argument is not an index. Optional thisArg is supplied as the callback's this; an arrow function retains its lexical this.
Iteration reflects changes: it skips values deleted before visitation and visits new values added before completion. A value already visited, then deleted and re-added, can be visited again. Such mutations can prevent termination.
return ends only the current callback invocation, not the traversal. Use for...of when you need break. An exception stops traversal. An incompatible receiver without internal Set data or a non-callable callback causes TypeError.
Examples
const values = new Set([3, 1, 3]);
const lines = [];
const result = values.forEach((value, valueAgain, set) => {
lines.push(`${value}|${valueAgain}|${set === values}`);
});
console.log(lines.join('\n'));
console.log(String(result));Result
3|3|true 1|1|true undefined
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.