CKTIS / JavaScript
Set.prototype.union — set union
Set.prototype.union(other) creates a new Set containing values from both sets without duplicates. It preserves the left set's order, then appends new values from other. The argument must support the set-like object protocol, not merely iteration.
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.union(other)Set.prototype.union(other) computes a union: it returns a new Set containing values present in at least one input. It first preserves the receiver's insertion order, then appends missing values in the order returned by other.keys(). The algorithm itself does not modify the input sets; custom getters and methods on other can nevertheless have side effects.
The receiver must be a genuine Set object with internal set data. other may be a set-like object: it must provide a valid size and callable has and keys methods; keys() must return an iterator. union validates has but does not call it. A Map satisfies this protocol — its keys are used. Being iterable alone is insufficient: an ordinary array is not a valid argument; use new Set(array).
Duplicates follow SameValueZero semantics: NaN values are treated as equal, as are +0 and -0; objects are compared by identity, not contents. The result is shallow: it holds the same object references. It is an ordinary Set, even when the receiver is a subclass instance.
The method is part of ECMAScript 2025, not a Web API. The language specification does not establish support in a particular browser; having Node 24 in a repository does not establish browser support either. Before use, check typeof Set.prototype.union === "function" in the target environment and provide a conforming fallback if needed.
Examples
const left = new Set([3, 1]);
const right = new Set([1, 2]);
const result = left.union(right);
console.log(JSON.stringify([...result]));
console.log(JSON.stringify([...left]));
console.log(JSON.stringify([...right]));
console.log(result !== left && result !== right);Result
[3,1,2] [3,1] [1,2] 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.