← blog
JavaScriptJuly 23, 2026 · 4 min read

You've been doing Set math by hand. JavaScript finally shipped .union(), .intersection(), and friends.

Set has always been missing its obvious operations. ES2025 adds `.union()`, `.intersection()`, `.difference()`, `.symmetricDifference()`, and three boolean predicates — native methods that used to require four lines of spread syntax each.

Parsa Jiravand · Frontend engineer · building bestpractic
You've been doing Set math by hand. JavaScript finally shipped `.union()`, `.intersection()`, and friends.

You've written some version of this:

JavaScript
1
2
3
const intersection = new Set([...setA].filter(x => setB.has(x))); const difference = new Set([...setA].filter(x => !setB.has(x))); const union = new Set([...setA, ...setB]);

It works. It's also four lines of manual iteration for operations any set-theory textbook covers in a sentence. JavaScript's Set shipped in ES6 — ten years ago — but the methods that make sets actually useful didn't come with it.

ES2025 added them. All of them.

Every new Set method takes another set-like object — a Set, a Map, or anything with a size property and a has method — and returns a new Set without modifying either input.

.union(other) — all elements from both sets:

JavaScript
1
2
3
4
const a = new Set([1, 2, 3]); const b = new Set([3, 4, 5]); a.union(b); // Set {1, 2, 3, 4, 5}

.intersection(other) — only elements present in both:

JavaScript
a.intersection(b); // Set {3}

.difference(other) — elements in this that are not in other:

JavaScript
1
2
a.difference(b); // Set {1, 2} b.difference(a); // Set {4, 5}

.symmetricDifference(other) — elements in exactly one of the two sets:

JavaScript
a.symmetricDifference(b); // Set {1, 2, 4, 5}

These four cover the standard set algebra you reach for most often. The other three are boolean predicates:

.isSubsetOf(other) — true if every element of this is in other:

JavaScript
1
2
new Set([1, 2]).isSubsetOf(new Set([1, 2, 3])); // true new Set([1, 4]).isSubsetOf(new Set([1, 2, 3])); // false

.isSupersetOf(other) — true if every element of other is in this:

JavaScript
new Set([1, 2, 3]).isSupersetOf(new Set([1, 2])); // true

.isDisjointFrom(other) — true if the sets share no elements at all:

JavaScript
1
2
new Set([1, 2]).isDisjointFrom(new Set([3, 4])); // true new Set([1, 2]).isDisjointFrom(new Set([2, 3])); // false

Runs right in your browser — poke at it and watch the concept react live.

The operations pay for themselves quickly when you're comparing sets of strings:

JavaScript
1
2
3
4
5
6
7
8
const currentPermissions = new Set(['read', 'write', 'admin']); const requiredPermissions = new Set(['read', 'write', 'delete']); const missing = requiredPermissions.difference(currentPermissions); // Set {'delete'} — what the user needs but doesn't have const hasAll = requiredPermissions.isSubsetOf(currentPermissions); // false — deny access

Or computing which tags changed between two versions of a document:

JavaScript
1
2
3
4
5
6
const before = new Set(['react', 'typescript', 'css']); const after = new Set(['react', 'vue', 'css', 'tailwind']); const added = after.difference(before); // Set {'vue', 'tailwind'} const removed = before.difference(after); // Set {'typescript'} const stable = before.intersection(after); // Set {'react', 'css'}

Before, each of those lines was a three-liner. Now each is one method call that reads exactly like what it computes.

Each method accepts any set-like object — not just Set instances. The spec defines set-like as any object with a numeric size property, a has(key) method, and a keys() method returning an iterator.

This means you can pass a Map as the argument and it works using the map's keys:

JavaScript
1
2
3
4
const activeUserIds = new Map([['u1', userA], ['u2', userB]]); const bannedIds = new Set(['u2', 'u3']); bannedIds.intersection(activeUserIds); // Set {'u2'}

It also means you can build custom data structures that interoperate with the native methods without converting to a plain Set first — anything implementing the three-property contract is compatible.

TypeScript added the full method signatures in version 5.5 under the ES2025 lib. If your tsconfig.json targets an earlier version, you'll see type errors. The fix is adding "ES2025" (or the more specific "ES2025.Collection") to your lib array:

JSON
1
2
3
4
5
{ "compilerOptions": { "lib": ["DOM", "ES2025"] } }

The algebra methods return Set<T> and the predicates return boolean. No type assertions or manual casting needed.

All seven methods are Baseline 2025: Chrome 122, Firefox 127, Safari 17.4, Node.js 22. Any environment targeting browsers from the last year ships these with no polyfill and no build step.

If you need to support older targets, a shim is a few dozen lines — but every major browser in active use today already has them natively.

Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.

Search your codebase for filter(x => otherSet.has(x)) or new Set([...a, ...b]). Each one is a native Set method written by hand.

The new methods aren't just shorter — they're clearer. .difference() names the operation. A spread with a filter describes the implementation. When both are available, the one that names the operation wins: it reads faster, it refactors cleaner, and it signals intent to the next person in the file. The only reason to reach for the manual version now is a polyfill budget you almost certainly don't have.


Thanks for reading! Let's stay connected:

Originally published on dev.to