Sub topic 3
Combining Sets with union
This chapter assumes you can create a Set, add values with add, check them with has, and iterate with for...of. It also assumes you know spread syntax ([...set]). The union method arrived in ES2025. It ships in current versions of Chrome, Edge, Firefox, Safari, and Node.js 22 and later.
What union does
union returns a new Set that holds every element found in either the receiver (the set you call the method on) or the argument. Each value appears only once, because a Set never holds duplicates. Neither input changes.
const frontend = new Set(["js", "css", "html"]);
const backend = new Set(["js", "sql"]);
const all = frontend.union(backend);
console.log([...all]); // ["js", "css", "html", "sql"]
console.log(frontend.size); // 3 (unchanged)
console.log(backend.size); // 2 (unchanged)Both sets contain "js", but the result holds it once. The result is a fresh object, so all === frontend is false. Adding to all later has no effect on frontend or backend.
How the result is built
The algorithm is simple, and knowing it lets you predict the output order:
- Copy every element of the receiver into a new set, in the receiver's insertion order.
- Walk the argument's elements in its own order.
- Add each one that is not already present. Values already present keep their original position.
So the receiver's elements always come first. That makes union commutative in content but not in order:
const a = new Set([3, 1]);
const b = new Set([2, 3]);
console.log([...a.union(b)]); // [3, 1, 2]
console.log([...b.union(a)]); // [2, 3, 1]The two results contain the same three numbers, so a size or has check treats them as equal. If you render the result in a list or compare it to a snapshot in a test, the order of the call matters.
Combining more than two sets
union takes exactly one argument. To merge several sets, chain the calls or reduce over an array of sets:
const team1 = new Set(["ana", "ben"]);
const team2 = new Set(["ben", "cy"]);
const team3 = new Set(["dee", "ana"]);
const chained = team1.union(team2).union(team3);
console.log([...chained]); // ["ana", "ben", "cy", "dee"]
const teams = [team1, team2, team3];
const reduced = teams.reduce((acc, s) => acc.union(s), new Set());
console.log([...reduced]); // ["ana", "ben", "cy", "dee"]Each call creates an intermediate set. For a handful of sets this costs nothing worth worrying about. If you are merging thousands of sets in a hot loop, adding into one mutable set with add avoids the extra allocations.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Expecting `a.union(b)` to change `a` | `a` is unchanged and the result is discarded | Assign it: `a = a.union(b)` or `const merged = a.union(b)` |
| Passing an array: `a.union([1, 2])` | `TypeError`, because arrays have no `size` property | Wrap it: `a.union(new Set([1, 2]))` |
| Assuming the order is sorted | The order follows insertion, receiver first | Sort explicitly: `[...result].sort()` |
| Expecting objects with equal contents to merge | Both objects stay, because elements are compared by reference | Use primitive keys such as IDs |
Exercises
Exercise
Given const x = new Set(["b", "a"]) and const y = new Set(["c", "a", "d"]), what does [...y.union(x)] log?
Show answer
["c", "a", "d", "b"]. The receiver y's elements come first in their insertion order. Then x is walked: "b" is new and is appended, and "a" is already present and is skipped.
Exercise
A developer writes tags.union(newTags); and later finds that tags.size has not grown. Why, and how should the line be fixed?
Show answer
union never mutates the receiver. It returns a new Set, and here that result is thrown away. Assign it, for example tags = tags.union(newTags) (with tags declared using let), or store it in a new variable.
Exercise
Write one expression that merges the sets in an array called groups into a single Set, and say what it returns when groups is empty.
Show answer
groups.reduce((acc, s) => acc.union(s), new Set()). With an empty array, reduce returns the initial value, which is an empty Set.