JavaScript Set difference() Method

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

Definition

set.difference(other) returns a new Set that holds the values of set that are not in other. Think of it as "set minus other": take the first Set and remove everything that also appears in the second.

Neither Set is changed. The result is a separate Set, and its values keep the order of the first Set. The argument can be another Set, or a set-like object such as a Map. An array is not allowed.

Remember
a.difference(b)   → values in a that are not in b

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

a.difference(b)   → {1, 2}
b.difference(a)   → {5, 6}   (not the same!)

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 all = new Set(["apple", "banana", "cherry"]);
const eaten = new Set(["banana"]);

const left = all.difference(eaten);

console.log(Array.from(left).join(", "));   // apple, cherry
console.log(all.size);                      // 3 (unchanged)
CallWhat it does
a.difference(b)A new Set with the values of a that are not in b.
b.difference(a)The other direction. The answer is usually different.
a.difference(map)A Map works as the argument, and its keys are compared.
a.difference(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(), entries(), forEach(), has(), intersection(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), keys(), symmetricDifference(), union(), values(), [Symbol.iterator]().

Library declarations
JavaScript
// ECMAScript 2025
Set.prototype.difference(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 of the first Set that are missing from other, in the order of the first Set. 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 — Subtract One Set from Another

Start with all the fruit, take away the fruit that was eaten, and see what is left.

JavaScript
const all = new Set(["apple", "banana", "cherry", "date"]);
const eaten = new Set(["banana", "date"]);

const left = all.difference(eaten);

console.log("Left: " + Array.from(left).join(", "));
console.log("Left size: " + left.size);
console.log("All size: " + all.size);
console.log("Eaten size: " + eaten.size);
Try It Yourself

The Try It page first checks that Set.prototype.difference() 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. Two Sets. all has four fruits and eaten has two of them.
  2. Subtract. all.difference(eaten) keeps the fruits that are not in eaten, which are apple and cherry, in their original order.
  3. Nothing is changed. The answer is a new Set. all still has 4 values and eaten still has 2.

Example 2 — Direction Matters

Subtract in both directions and see that the answers are different. A Set minus itself is always empty.

JavaScript
const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5, 6]);

console.log("a minus b: " + Array.from(a.difference(b)).join(", "));
console.log("b minus a: " + Array.from(b.difference(a)).join(", "));
console.log("a minus a: " + a.difference(a).size);
console.log("a minus empty: " + a.difference(new Set()).size);
Try It Yourself

The Try It page first checks that Set.prototype.difference() 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. a minus b. The numbers 3 and 4 are in both Sets, so they are removed from a. 1, 2 are left.
  2. b minus a. Going the other way removes the same shared numbers from b, and leaves 5, 6. The two answers are different.
  3. Two special cases. A Set minus itself has nothing left, so the size is 0. A Set minus an empty Set has nothing to remove, so all 4 values stay.

Example 3 — Use a Map as the Other Set

The argument only needs to be set-like, so a Map works too, and its keys are used. An array is rejected. The catch block prints the error type only.

JavaScript
const invited = new Set(["Ann", "Bob", "Cy", "Di"]);
const declined = new Map([["Bob", "busy"], ["Di", "away"]]);

const coming = invited.difference(declined);

console.log("Coming: " + Array.from(coming).join(", "));
console.log("Invited size: " + invited.size);

try {
  invited.difference(["Bob"]);
} catch (err) {
  console.log("With an array: " + err.name);
}

const fixed = invited.difference(new Set(["Bob"]));
console.log("Array fixed: " + Array.from(fixed).join(", "));
Try It Yourself

The Try It page first checks that Set.prototype.difference() 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. A Map is set-like. It has size, has() and keys(), so invited.difference(declined) compares the Map keys Bob and Di and removes them.
  2. An array is not. An array has no has() method, so passing ["Bob"] throws a TypeError.
  3. The fix. Wrap the array in new Set(...). Then only Bob is removed, and Ann, Cy, Di are left.

Browser & Runtime Support

Set.prototype.difference() 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.difference()

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.difference() Recent browsers

Bottom line: Check support with typeof Set.prototype.difference === "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 the first Set but not in the second one. Neither Set is changed. It is the Set version of "A minus B".
No. a.difference(b) keeps what only a has, and b.difference(a) keeps what only b has. If you want both together, use symmetricDifference().
No. The argument must be a Set or a set-like object, such as a Map, because the method needs size, has() and keys(). Passing an array throws a TypeError. Use new Set(array) to convert it first.
No. It creates and returns a new Set. The Set you call it on and the argument keep all their values.
Use filter() on the values of the first Set: new Set([...a].filter((x) => !b.has(x))). It gives the same Set and works everywhere that Set works.

Next: entries()

Learn how to loop over a Set as pairs of [value, value], the same shape that a Map uses.

entries() 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.

11 people found this page helpful