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:
_10const frontend = new Set(["ada", "grace", "linus"]);_10const backend = new Set(["grace", "linus", "ken"]);_10_10const both = new Set([...frontend].filter((name) => backend.has(name)));_10const 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
_14const frontend = new Set(["ada", "grace", "linus"]);_14const backend = new Set(["grace", "linus", "ken"]);_14_14frontend.union(backend);_14// Set { "ada", "grace", "linus", "ken" } everyone on either team_14_14frontend.intersection(backend);_14// Set { "grace", "linus" } on both teams_14_14frontend.difference(backend);_14// Set { "ada" } frontend only_14_14frontend.symmetricDifference(backend);_14// 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
_10const required = new Set(["read", "write"]);_10const granted = new Set(["read", "write", "delete"]);_10const banned = new Set(["impersonate"]);_10_10required.isSubsetOf(granted); // true: every required permission is granted_10granted.isSupersetOf(required); // true: the same check from the other side_10granted.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
sizeproperty - 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.
_11const tags = new Set(["css", "html"]);_11_11tags.union(["javascript"]);_11// TypeError: the argument is not set-like_11_11tags.union(new Set(["javascript"]));_11// Set { "css", "html", "javascript" }_11_11const counts = new Map([["css", 3], ["react", 5]]);_11tags.intersection(counts);_11// 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:
_10const evens = {_10 size: Infinity,_10 has: (n) => Number.isInteger(n) && n % 2 === 0,_10 keys: () => {_10 throw new Error("not iterable");_10 },_10};_10_10new Set([1, 2, 3, 4]).intersection(evens);_10// 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.
_10const before = new Set(savedTagIds);_10const after = new Set(selectedTagIds);_10_10const added = after.difference(before);_10const removed = before.difference(after);_10_10await Promise.all([_10 ...added.values().map((id) => addTag(postId, id)),_10 ...removed.values().map((id) => removeTag(postId, id)),_10]);
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.
_10function canPublish(user) {_10 const needed = new Set(["posts:write", "posts:publish"]);_10 return needed.isSubsetOf(new Set(user.permissions));_10}
Finding shared interests
_10function sharedTopics(a, b) {_10 return new Set(a.topics).intersection(new Set(b.topics));_10}_10_10const overlap = sharedTopics(reader, article);_10if (overlap.size > 0) {_10 showRecommendation(article, [...overlap]);_10}
Feature flags across environments
_10const staging = new Set(["new-nav", "dark-mode", "search-v2"]);_10const production = new Set(["dark-mode"]);_10_10const notYetLive = staging.difference(production);_10// Set { "new-nav", "search-v2" }_10_10const drift = staging.symmetricDifference(production);_10// 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 })isfalse. If you need to compare records, build sets of their IDs. - NaN equals NaN, and 0 equals -0, as with all
Setoperations. - 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
Setkeeps one of each value. If you need to count how many times something appears, use aMapof 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
includescall 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,differenceandsymmetricDifferencereturn new sets.isSubsetOf,isSupersetOfandisDisjointFromreturn booleans.- None of them mutate the original set.
- The argument must be set-like, meaning it has
size,has()andkeys(). AMapworks, 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.
JavascriptWas this article helpful?

About the author
Danny Engineering
A software engineer with a strong belief in human-centric design and driven by a deep empathy for users. Combining the latest technology with human values to build a better, more connected world.