---
title: "The new Set methods: union, intersection, difference and more"
description: "JavaScript Sets now have union, intersection, difference, symmetricDifference and subset checks built in. Learn how they work, what set-like means, and where they are supported."
url: https://www.danny.engineering/article/the-new-set-methods-union-intersection-difference-and-more
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:23:00.000Z
tags: ["Javascript"]
---

# The new Set methods: union, intersection, difference and more

`Set` has been in JavaScript since 2015, but for most of that time it could only add, delete and check single values. Anything involving two sets, such as finding what they share or what one has that the other lacks, meant spreading into arrays and writing `filter` calls by hand. Those snippets are easy to get subtly wrong and easy to make slow.


Sets now come with seven methods for exactly these jobs. This article is for JavaScript developers who use `Set` for de-duplication and membership checks and want to go further. We will cover each method, the "set-like" rule for arguments, a few practical patterns, and where support stands.


## The old way


Here is how intersection and difference usually looked:


```javascript
const frontend = new Set(["ada", "grace", "linus"]);
const backend = new Set(["grace", "linus", "ken"]);

const both = new Set([...frontend].filter((name) => backend.has(name)));
const onlyFrontend = new Set([...frontend].filter((name) => !backend.has(name)));
```


It works, but each line copies a set into an array, filters it, and builds a new set. It also hides the intent behind boilerplate. A reader has to work out that the first line is an intersection.


## The seven methods


The new methods split into two groups. Four return a new `Set`. Three return a boolean. None of them change the set they are called on.


### Methods that return a new Set


```javascript
const frontend = new Set(["ada", "grace", "linus"]);
const backend = new Set(["grace", "linus", "ken"]);

frontend.union(backend);
// Set { "ada", "grace", "linus", "ken" }  everyone on either team

frontend.intersection(backend);
// Set { "grace", "linus" }  on both teams

frontend.difference(backend);
// Set { "ada" }  frontend only

frontend.symmetricDifference(backend);
// Set { "ada", "ken" }  on exactly one team
```


`difference` is the only one where order matters: `a.difference(b)` is what is in `a` but not `b`, which is different from `b.difference(a)`. The other three give the same members whichever way round you call them, though the order of items in the result can differ.


### Methods that return a boolean


```javascript
const required = new Set(["read", "write"]);
const granted = new Set(["read", "write", "delete"]);
const banned = new Set(["impersonate"]);

required.isSubsetOf(granted);   // true: every required permission is granted
granted.isSupersetOf(required); // true: the same check from the other side
granted.isDisjointFrom(banned); // true: no overlap at all
```


These replace loops like `[...required].every((p) => granted.has(p))`, and they stop as soon as the answer is known.


## The argument must be set-like


The value you pass in does not have to be a `Set`, but it has to look like one. In the spec this is called set-like, and it means the object has:

- a numeric `size` property
- a `has()` method
- a `keys()` method that returns an iterator

A `Map` meets this, using its keys. An array does not, because it has no `size` or `has`. Passing one throws a `TypeError`.


```javascript
const tags = new Set(["css", "html"]);

tags.union(["javascript"]);
// TypeError: the argument is not set-like

tags.union(new Set(["javascript"]));
// Set { "css", "html", "javascript" }

const counts = new Map([["css", 3], ["react", 5]]);
tags.intersection(counts);
// Set { "css" }  uses the Map's keys
```


Wrap arrays in `new Set(...)` first.


The reason for this rule is performance. The algorithms look at the sizes of both sides and choose which one to loop over. For example, an intersection may iterate the smaller collection and call `has()` on the larger one. That is also why a custom set-like object must report an honest `size`.


You can write your own set-like object when the full set is large or virtual:


```javascript
const evens = {
  size: Infinity,
  has: (n) => Number.isInteger(n) && n % 2 === 0,
  keys: () => {
    throw new Error("not iterable");
  },
};

new Set([1, 2, 3, 4]).intersection(evens);
// Set { 2, 4 }
```


Because `evens` is reported as infinitely large, `intersection` loops over the small set and only calls `has()`, so `keys()` is never needed. This is a neat trick, but use it with care: methods such as `union` do need to iterate the argument.


## Real-world patterns


### Diffing selections


When a user edits a multi-select, you often need to know what was added and what was removed so you can send only the changes.


```javascript
const before = new Set(savedTagIds);
const after = new Set(selectedTagIds);

const added = after.difference(before);
const removed = before.difference(after);

await Promise.all([
  ...added.values().map((id) => addTag(postId, id)),
  ...removed.values().map((id) => removeTag(postId, id)),
]);
```


The `values().map(...)` call uses iterator helpers, which arrived in browsers around the same time. You could use `Array.from(added, fn)` instead.


### Permission checks


Subset checks read almost like the requirement itself.


```javascript
function canPublish(user) {
  const needed = new Set(["posts:write", "posts:publish"]);
  return needed.isSubsetOf(new Set(user.permissions));
}
```


### Finding shared interests


```javascript
function sharedTopics(a, b) {
  return new Set(a.topics).intersection(new Set(b.topics));
}

const overlap = sharedTopics(reader, article);
if (overlap.size > 0) {
  showRecommendation(article, [...overlap]);
}
```


### Feature flags across environments


```javascript
const staging = new Set(["new-nav", "dark-mode", "search-v2"]);
const production = new Set(["dark-mode"]);

const notYetLive = staging.difference(production);
// Set { "new-nav", "search-v2" }

const drift = staging.symmetricDifference(production);
// any flag that is on in one place but not the other
```


## Things to know

- **Results are always new Set objects.** The original is never modified. If you want to add to a set in place, loop and call `add()`.
- **Membership uses SameValueZero.** Two objects with the same contents are different members. `new Set([{ id: 1 }]).has({ id: 1 })` is `false`. If you need to compare records, build sets of their IDs.
- **NaN equals NaN, and 0 equals -0,** as with all `Set` operations.
- **Results follow insertion order.** Items from the set you call the method on generally come first, but do not rely on a particular order if it matters to your program. Sort the result explicitly.
- **They are not on Map.** These methods belong to `Set.prototype`. To combine maps, work with their keys.

## Browser support


The Set methods became Baseline newly available on 11 June 2024, when Firefox shipped them. The first supporting versions are:

- Chrome and Edge 122
- Firefox 127
- Safari 17, on macOS and iOS

They are expected to reach Baseline widely available in December 2026. On the server, they are available in Node 22 and later, which covers every supported Node release.


TypeScript includes types for the new methods from version 5.5 when your `lib` or `target` is set to a recent ECMAScript version such as `esnext`. If your editor says `union` does not exist on `Set`, update your `lib` setting. If you need to support older browsers, core-js provides polyfills.


## When not to use it

- **Duplicates matter.** A `Set` keeps one of each value. If you need to count how many times something appears, use a `Map` of counts or work with arrays.
- **You are comparing objects by content.** Sets compare by identity. Build sets of IDs or other primitive keys, or use a different approach.
- **The data is already an array and the operation happens once on a few items.** Converting both sides to sets to check one overlap is fine, but a single `includes` call can be just as clear for tiny lists.
- **You need the order preserved in a specific way.** Sets keep insertion order, but the rules for combined results are easy to forget. Sort explicitly when order is part of the output.

## Wrapping up

- `union`, `intersection`, `difference` and `symmetricDifference` return new sets. `isSubsetOf`, `isSupersetOf` and `isDisjointFrom` return booleans.
- None of them mutate the original set.
- The argument must be set-like, meaning it has `size`, `has()` and `keys()`. A `Map` works, an array does not.
- Membership is by identity, so build sets of IDs when comparing records.
- Supported in Chrome and Edge 122, Firefox 127, Safari 17 and Node 22 and later.
