Sub topic 8
Union, Intersection, Difference, and Subset Checks
This chapter assumes you can create a Set, add values to it, and spread it into an array. Those topics were covered in earlier chapters. Here you combine and compare whole Sets with seven built-in methods that were added in ES2025. They are supported in Node.js 22+, Chrome 122+, Firefox 127+, and Safari 17+.
The working model
Every composition method is called on one Set (this) and takes a second, set-like argument (other). Each method returns either a new Set or a boolean. Neither input Set is modified.
| Method | Returns | Contains or checks |
|---|---|---|
| `a.union(b)` | Set | Values in `a` or `b` |
| `a.intersection(b)` | Set | Values in both |
| `a.difference(b)` | Set | Values in `a` but not `b` |
| `a.symmetricDifference(b)` | Set | Values in exactly one of them |
| `a.isSubsetOf(b)` | boolean | Every value of `a` is in `b` |
| `a.isSupersetOf(b)` | boolean | Every value of `b` is in `a` |
| `a.isDisjointFrom(b)` | boolean | No shared values |
Combining Sets
const a = new Set([1, 2, 3]);
const b = new Set([2, 3, 4]);
console.log([...a.union(b)]); // [1, 2, 3, 4]
console.log([...a.intersection(b)]); // [2, 3]
console.log([...a.difference(b)]); // [1]
console.log([...a.symmetricDifference(b)]); // [1, 4]
console.log(a.isSubsetOf(new Set([1, 2, 3, 4]))); // true
console.log(a.isSupersetOf(new Set([1, 2]))); // true
console.log(a.isDisjointFrom(new Set([7, 8]))); // true
console.log([...a]); // [1, 2, 3] (unchanged)Values are matched with SameValueZero, the same rule has uses. Objects therefore match only by reference.
A practical example: permission checks
const required = new Set(['read', 'write']);
const granted = new Set(['read', 'comment']);
if (!required.isSubsetOf(granted)) {
const missing = required.difference(granted);
console.log('Missing:', [...missing]); // Missing: [ 'write' ]
}Common errors
Passing an array
The argument must be set-like. That means it needs a numeric size, a has method, and a keys method. Arrays have no has method or size property, so passing one throws.
const tags = new Set(['js', 'css']);
try {
tags.union(['html']);
} catch (err) {
console.log(err instanceof TypeError); // true
}
console.log([...tags.union(new Set(['html']))]); // ['js', 'css', 'html']Wrap the array in new Set(...) first. A Map is set-like: its keys are used and its values are ignored.
Relying on result order
union and difference keep this's insertion order, and union adds new values from other after it. intersection is different. To save work, the engine loops over whichever Set is smaller, so the result's order can follow either input. If order matters, sort the result yourself.
Expecting mutation
a.union(b) does not change a. To update a, assign the result back (a = a.union(b)), or loop over b and call add on a.
Exercise
Given const enrolled = new Set(['ana', 'ben', 'cho']) and const attended = new Set(['ben', 'dev']), write expressions for (1) students who enrolled but did not attend, and (2) whether everyone who attended was enrolled.
Show answer
(1) enrolled.difference(attended) gives Set {'ana', 'cho'}. (2) attended.isSubsetOf(enrolled) gives false, because 'dev' attended without enrolling.
Exercise
Why does new Set([1, 2]).intersection([2, 3]) throw, and how do you fix it?
Show answer
The argument is an array, which is not set-like because it has no size property and no has method, so a TypeError is thrown. Pass new Set([2, 3]) instead, which returns Set {2}.