TL;DR
Learn how JavaScript’s seven new Set methods—difference
, intersection
, union
, symmetricDifference
, isDisjointFrom
, isSubsetOf
, and isSupersetOf
—let you write cleaner, more expressive code.
Introduction
In ES6, the Set object became a staple for storing unique values, but until recently, combining or comparing sets required clunky loops or array conversions. You might have written code like this to get the intersection of two sets:
const intersection = new Set(
[...setA].filter(x => setB.has(x))
);
Thankfully, JavaScript now includes built-in methods that make these operations concise and self-documenting. In this article, we’ll explore seven powerful Set methods—difference()
, intersection()
, union()
, symmetricDifference()
, isDisjointFrom()
, isSubsetOf()
, and isSupersetOf()
—to help you refactor old code and simplify your data-structure logic.
Deep Dive: The 7 New Set Methods
For each method below you’ll see:
1. Signature
2. What it does
3. Example
4. When to use it
1. difference()
Set.prototype.difference(otherSet) → Set
What it does:
Returns a new Set containing all values in the original set that are not in otherSet.Example
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5]);
const onlyInA = setA.difference(setB);
console.log(onlyInA); // Set {1, 2}
- When to use it Filtering out unwanted values from one collection based on another.
2. intersection()
Set.prototype.intersection(otherSet) → Set
What it does:
Returns a new Set containing only the values found in both sets.Example
const setA = new Set(['apple', 'banana', 'cherry']);
const setB = new Set(['banana', 'dragonfruit', 'apple']);
const common = setA.intersection(setB);
console.log(common); // Set {'apple', 'banana'}
- When to use it Finding overlaps between two datasets (e.g., shared tags, common permissions).
3. union()
Set.prototype.union(otherSet) → Set
What it does:
Returns a new Set containing all unique values from both sets.Example
const setA = new Set([1, 2]);
const setB = new Set([2, 3, 4]);
const all = setA.union(setB);
console.log(all); // Set {1, 2, 3, 4}
- When to use it Merging data streams or combining feature-flag sets.
4. symmetricDifference()
Set.prototype.symmetricDifference(otherSet) → Set
What it does:
Returns a new Set of values that are in either set but not both.Example
const setA = new Set(['x', 'y', 'z']);
const setB = new Set(['y', 'w']);
const diff = setA.symmetricDifference(setB);
console.log(diff); // Set {'x', 'z', 'w'}
- When to use it Identifying elements that differ between two configurations or snapshots.
5. isDisjointFrom()
Set.prototype.isDisjointFrom(otherSet) → boolean
What it does:
Returnstrue
if the two sets share no common elements.Example
const setA = new Set([10, 20]);
const setB = new Set([30, 40]);
console.log(setA.isDisjointFrom(setB)); // true
const setC = new Set([20, 50]);
console.log(setA.isDisjointFrom(setC)); // false
- When to use it Quickly check whether two collections overlap at all (e.g., scheduling conflicts).
6. isSubsetOf()
Set.prototype.isSubsetOf(otherSet) → boolean
What it does:
Returnstrue
if every element in the original set is also in otherSet.Example
const small = new Set([1, 2]);
const large = new Set([1, 2, 3]);
console.log(small.isSubsetOf(large)); // true
const weird = new Set([2, 4]);
console.log(weird.isSubsetOf(large)); // false
- When to use it Validating permission hierarchies or ensuring one dataset is contained in another.
7. isSupersetOf()
Set.prototype.isSupersetOf(otherSet) → boolean
What it does:
Returnstrue
if the original set contains every element of otherSet.Example
const roles = new Set(['admin', 'editor', 'viewer']);
const needed = new Set(['editor', 'viewer']);
console.log(roles.isSupersetOf(needed)); // true
const extra = new Set(['editor', 'contributor']);
console.log(roles.isSupersetOf(extra)); // false
- When to use it Checking that a user’s permissions include all required rights for a given action.
Practical Use Cases
1.De-duplicating Combined Data Sources
When you merge results from multiple APIs or database queries, you often need to remove duplicates. Instead of mapping to arrays and back, use union() directly:
const apiA = new Set(await fetchUsersFromServiceA());
const apiB = new Set(await fetchUsersFromServiceB());
const uniqueUsers = apiA.union(apiB);
2.Permission and Role Checks
Model user roles or feature toggles as sets, then use isSupersetOf() to verify access:
const userRoles = new Set(['reader', 'commenter']);
const required = new Set(['reader']);
if (!userRoles.isSupersetOf(required)) {
throw new Error('Insufficient permissions');
}
3.Comparing Configurations or Feature Flags
Quickly detect what settings have changed between two snapshots using symmetricDifference():
const oldFlags = new Set(['beta', 'darkMode']);
const newFlags = new Set(['darkMode', 'logging']);
const changed = oldFlags.symmetricDifference(newFlags);
console.log(changed); // Set {'beta', 'logging'}
4.Filtering Out Unwanted Items
Remove deprecated IDs or blacklisted entries with difference():
const allItems = new Set(getAllItemIds());
const deprecated = new Set(getDeprecatedIds());
const activeItems = allItems.difference(deprecated);
5.Overlap and Conflict Detection
Use isDisjointFrom()
to verify that two schedules, seat assignments, or resource pools don’t collide:
const morningSlots = new Set(['09:00', '10:00', '11:00']);
const bookedSlots = new Set(['11:00', '12:00']);
if (!morningSlots.isDisjointFrom(bookedSlots)) {
console.log('Conflict at 11:00');
}
Conclusion
JavaScript’s new Set methods—difference()
, intersection()
, union()
, symmetricDifference()
, isDisjointFrom()
, isSubsetOf()
, and isSupersetOf()
—turn once-verbose patterns into clear, intent-driven calls.
Now that these operations are part of the language, it’s a great time to:
- Revisit your old code that relied on array hacks or manual loops
- Refactor your data-handling logic to use these self-documenting methods
- Share your favorite use cases and tips in the comments below!
Top comments (9)
Dude, finally, these new Set methods legit save me so much headache. Took way too long to get here but I’m glad it’s real now.
Totally! It’s such a relief to ditch those endless loops.
All the 7 methods are very useful! Thanks a lot!
That’s awesome to hear, glad you found them helpful! 🎉
good
These updates for Set are such a relief, finally makes things like difference and intersection readable. Have you noticed any surprising speed or edge cases using the new methods?
Good question! I haven’t run any formal benchmarks yet, so I can’t speak to raw speed.
Very helpful
Loved how clearly you broke it all down ..