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)
Call
What 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.
// 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
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 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);
Left: apple, cherry
Left size: 2
All size: 4
Eaten size: 2
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
Two Sets.all has four fruits and eaten has two of them.
Subtract.all.difference(eaten) keeps the fruits that are not in eaten, which are apple and cherry, in their original order.
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);
a minus b: 1, 2
b minus a: 5, 6
a minus a: 0
a minus empty: 4
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
a minus b. The numbers 3 and 4 are in both Sets, so they are removed from a. 1, 2 are left.
b minus a. Going the other way removes the same shared numbers from b, and leaves 5, 6. The two answers are different.
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.
Coming: Ann, Cy
Invited size: 4
With an array: TypeError
Array fixed: Ann, Cy, Di
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
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.
An array is not. An array has no has() method, so passing ["Bob"] throws a TypeError.
The fix. Wrap the array in new Set(...). Then only Bob is removed, and Ann, Cy, Di are left.
Compatibility
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.
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.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.