set.symmetricDifference(other) returns a new Set with the values that are in only one of the two Sets. Values that appear in both Sets are left out. It is like "A or B, but not both".
Neither Set is changed. The order is: first the values that only set has, then the values that only other has. The argument can be a Set or a set-like object such as a Map, but not an array.
Remember
a.symmetricDifference(b) → in a or in b, but not in both
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
a.symmetricDifference(b) → {1, 2, 5, 6}
3 and 4 are shared, so they are left out
a and b stay unchanged
Syntax
Call it on a Set and pass another Set. Read the answer from the new Set that comes back.
// ECMAScript 2025
Set.prototype.symmetricDifference(other)
// other : a Set, or a set-like object (needs size, has() and keys())
// returns: a new Set
// throws : TypeError if other is not set-like (for example an array)
Parameters
Parameter
Description
other
Required. A Set, or any set-like object: an object with a numeric size and the methods has() and keys(). A Map is set-like. An array is not, so convert it with new Set(array).
Return Value
Return Value
Description
Set
A new Set with the values that are in exactly one of the two Sets. It is empty when both Sets have the same values. Both original Sets stay as they were.
TypeError (thrown)
other is not set-like, for example an array or a number.
JavaScript Examples
Example 1 — Find the Values in Only One Set
Two people list the skills they know. Find the skills that only one of them has.
The Try It page first checks that Set.prototype.symmetricDifference() exists, because it is new in ES2025. In an older browser it prints a short explanation and the output above. In current browsers and in Node.js 22 or newer, it runs the code for real.
How It Works
Shared values are dropped.js and css are in both Sets, so they are not in the answer.
The rest stays.html is only in mine and python is only in yours, so both are returned.
Originals are safe.mine still has 3 values, because the method returns a new Set.
Example 2 — Both Directions and Special Cases
The two directions give the same values in a different order. Then try the same Set and an empty Set.
JavaScript
const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5, 6]);
console.log("a, b: " + Array.from(a.symmetricDifference(b)).join(", "));
console.log("b, a: " + Array.from(b.symmetricDifference(a)).join(", "));
console.log("a, a: " + a.symmetricDifference(a).size);
console.log("a, empty: " + Array.from(a.symmetricDifference(new Set())).join(", "));
a, b: 1, 2, 5, 6
b, a: 5, 6, 1, 2
a, a: 0
a, empty: 1, 2, 3, 4
The Try It page first checks that Set.prototype.symmetricDifference() exists, because it is new in ES2025. In an older browser it prints a short explanation and the output above. In current browsers and in Node.js 22 or newer, it runs the code for real.
How It Works
Same values. In both directions, 3 and 4 are dropped, and 1, 2, 5, 6 are kept.
Different order. The values of the Set you start from come first: 1, 2 then 5, 6 in one case, and 5, 6 then 1, 2 in the other.
Special cases. A Set against itself shares everything, so nothing is left. A Set against an empty Set shares nothing, so every value stays.
Example 3 — Compare Two Lists
Find what changed between an old and a new list, and check whether two Sets hold the same values.
JavaScript
const before = new Set(["a.txt", "b.txt", "c.txt"]);
const after = new Set(["b.txt", "c.txt", "d.txt"]);
const changes = before.symmetricDifference(after);
console.log("Changed: " + Array.from(changes).join(", "));
const copy = new Set(["c.txt", "b.txt", "a.txt"]);
const same = before.symmetricDifference(copy).size === 0;
console.log("Same values: " + same);
console.log("Same as after: " + (before.symmetricDifference(after).size === 0));
Changed: a.txt, d.txt
Same values: true
Same as after: false
The Try It page first checks that Set.prototype.symmetricDifference() exists, because it is new in ES2025. In an older browser it prints a short explanation and the output above. In current browsers and in Node.js 22 or newer, it runs the code for real.
How It Works
Find the changes.a.txt was removed and d.txt was added. Both are in only one list, so both are returned.
Check for equality. If the result is empty, the Sets have exactly the same values. copy has the same files in a different order, so its size is 0.
Order does not matter. A Set does not care about the order of values for this check, so copy counts as equal to before, but after does not.
Compatibility
Browser & Runtime Support
Set.prototype.symmetricDifference() is one of the new Set methods added in ES2025. It works in current browsers and in Node.js 22 or newer, but not in older versions.
✓ Newer · ES2025
set.symmetricDifference()
Supported in Chrome 122+, Edge 122+, Firefox 127+, Safari 17+, Opera 108+ and Node.js 22+. Internet Explorer never got it.
ES2025Standard since
Google Chrome122+ · Desktop & Mobile
Full support
Microsoft Edge122+ · Chromium based
Full support
Mozilla Firefox127+ · Desktop & Mobile
Full support
Apple Safari17+ · macOS & iOS
Full support
Opera108+ · Chromium based
Full support
Samsung Internet26+ · Android
Supported
Android WebView122+ · Chromium based
Supported
Node.js22+ · Server runtime
Supported
DenoRecent versions · JavaScript runtime
Supported
BunRecent versions · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
set.symmetricDifference()Recent browsers
Bottom line: Check support with typeof Set.prototype.symmetricDifference === "function". For older browsers, the same result can be made with the spread syntax and filter(), or with a loop.
Frequently Asked Questions
It returns a new Set with the values that are in exactly one of the two Sets. Values that both Sets have are left out. Neither original Set is changed.
a.difference(b) keeps only what a has and b does not. a.symmetricDifference(b) keeps what only a has and also what only b has. It does not matter which Set you start from, except for the order.
Check that a.symmetricDifference(b).size is 0. Order does not matter for this check. You can also compare the sizes and use isSubsetOf().
No. The argument must be a Set or a set-like object such as a Map. An array throws a TypeError. Use new Set(array) to convert it first.
Spread each Set into an array and filter both: new Set([...[...a].filter((x) => !b.has(x)), ...[...b].filter((x) => !a.has(x))]). It gives the same values and works everywhere that Set works.