Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Set.prototype.symmetricDifference — symmetric difference of sets

If the input elements remain unchanged during the call, symmetricDifference() returns a new Set containing values present in exactly one of the two sets. The method itself does not modify the input sets.

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.symmetricDifference(other)

If the input elements remain unchanged during the call, a.symmetricDifference(b) returns a new Set containing values belonging to a or b, but not both. For a set-like argument, its elements are the values supplied by keys(). Unlike a one-sided difference, the symmetric difference includes elements unique to either side. The method itself does not modify the input sets.

The this receiver must be an actual Set object. The other argument can be a set-like object: it must expose a size property and callable has and keys methods. Converting size to a number must not yield NaN, and the result must not be negative after truncation toward zero. Thus, a size of -0.5 is accepted if the rest of the interface is valid. The algorithm validates this interface but obtains values through keys(), not through has(). A Map qualifies, with its keys acting as elements here; an ordinary array does not satisfy this interface. Custom argument accessors and methods can have side effects.

The algorithm copies the receiver's data before first advancing the argument's iterator, but checks each yielded key for membership in the live receiver, not in the copy. If the iterator changes the receiver's contents, the result need not be the symmetric difference between the yielded keys and either the original or the final receiver contents.

With stable inputs, the result first preserves the order of values unique to the receiver, then the order of values unique to other, as supplied by keys(). Equality follows SameValueZero: NaN matches NaN, +0 and -0 are equivalent, and objects are compared by identity rather than contents.

The method is part of ECMAScript 2025, not a separate Web API. The cited specification does not provide a browser compatibility table; before deployment, check typeof Set.prototype.symmetricDifference === "function" in the target environment. Node 24 in the repository does not prove browser support.

Examples

const a = new Set([1, 2, 3]);
const b = new Set([3, 4, 2, 5]);
const result = a.symmetricDifference(b);

console.log(JSON.stringify([...result]));
console.log(JSON.stringify([...a]));
console.log(JSON.stringify([...b]));

Result

[1,4,5]
[1,2,3]
[3,4,2,5]

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