JavaScript Set symmetricDifference() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Instance method

Definition

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.

JavaScript
const mine = new Set(["js", "css", "html"]);
const yours = new Set(["css", "python", "js"]);

const onlyOne = mine.symmetricDifference(yours);

console.log(Array.from(onlyOne).join(", "));   // html, python
CallWhat it does
a.symmetricDifference(b)A new Set with the values that only one of the two Sets has.
b.symmetricDifference(a)The same values. Only the order can be different.
a.union(b).difference(a.intersection(b))The same result built from three other methods.
a.symmetricDifference(new Set(array))Turn an array into a Set first, because an array is not accepted.

A standard JavaScript method (ES2025) on every Set, from Set.prototype. It works in current browsers and in Node.js 22 or newer. Siblings: add(), clear(), delete(), difference(), entries(), forEach(), has(), intersection(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), keys(), union(), values(), [Symbol.iterator]().

Library declarations
JavaScript
// 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

ParameterDescription
otherRequired. 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 ValueDescription
SetA 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.

JavaScript
const mine = new Set(["js", "css", "html"]);
const yours = new Set(["css", "python", "js"]);

const onlyOne = mine.symmetricDifference(yours);

console.log("Only one: " + Array.from(onlyOne).join(", "));
console.log("Size: " + onlyOne.size);
console.log("Mine size: " + mine.size);
Try It Yourself

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

  1. Shared values are dropped. js and css are in both Sets, so they are not in the answer.
  2. The rest stays. html is only in mine and python is only in yours, so both are returned.
  3. 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(", "));
Try It Yourself

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

  1. Same values. In both directions, 3 and 4 are dropped, and 1, 2, 5, 6 are kept.
  2. 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.
  3. 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));
Try It Yourself

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

  1. Find the changes. a.txt was removed and d.txt was added. Both are in only one list, so both are returned.
  2. 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.
  3. 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.

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.

ES2025 Standard since
Google Chrome 122+ · Desktop & Mobile
Full support
Microsoft Edge 122+ · Chromium based
Full support
Mozilla Firefox 127+ · Desktop & Mobile
Full support
Apple Safari 17+ · macOS & iOS
Full support
Opera 108+ · Chromium based
Full support
Samsung Internet 26+ · Android
Supported
Android WebView 122+ · Chromium based
Supported
Node.js 22+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No 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.

Next: union()

Learn how to join the values of two Sets into one new Set without duplicates.

union() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful