CKTIS / JavaScript
Set.prototype.intersection — common set elements
The intersection(other) method returns a new Set containing values shared by both collections. Result order depends on the operands’ sizes; other may also be an object exposing the set-like interface.
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.intersection(other)set.intersection(other) computes an intersection: it returns a new Set containing values present in both collections. With ordinary sets, it changes neither input. The method is part of ECMAScript 2025, not a browser API. The specification alone does not establish support in a particular browser; check typeof Set.prototype.intersection === "function" before use. Node 24 in a repository does not prove browser support.
The receiver set must be an actual Set object. The argument other may be a set or a set-like object: it must expose a valid size and callable has and keys methods. A Map also qualifies, with its keys treated as elements. Being iterable is not enough: an ordinary array does not implement this interface. An invalid receiver causes a TypeError.
For ordinary sets, the method iterates the smaller set and preserves its insertion order. When sizes are equal, it uses the order of set. Consequently, a.intersection(b) and b.intersection(a) can contain the same elements in different orders. With ordinary sets, membership testing follows SameValueZero semantics: NaN matches NaN, +0 and -0 are equivalent, and objects must have the same identity. When the receiver’s size is less than or equal to the size reported by other, the method tests membership by calling other.has. A custom has need not follow SameValueZero and can, for example, treat distinct objects as equivalent.
In the example, right is smaller, so the result order is [3,2], while the input sizes stay unchanged. Custom getters and the has and keys methods of other can execute code, mutate data, or throw exceptions; do not assume the operation is free of side effects in that case.
Examples
const left = new Set([1, 2, 3, 4]);
const right = new Set([3, 2]);
const common = left.intersection(right);
console.log(JSON.stringify([...common]));
console.log(left.size, right.size);Result
[3,2] 4 2
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.