JavaScript Reference ยท Collections
Set union, intersection and difference
Reference for the JavaScript Set methods union(), intersection(), difference(), symmetricDifference(), isSubsetOf(), isSupersetOf() and isDisjointFrom().
Recent versions of JavaScript give Set seven methods from set theory. Four of them build and return a new set: union(), intersection(), difference() and symmetricDifference(). Three answer a yes/no question: isSubsetOf(), isSupersetOf() and isDisjointFrom().
None of them change the sets they are called on. The argument must be a "set-like" object: a Set, or a Map, or anything with a numeric size, a has() method and a keys() method. A plain array is not accepted, so wrap it with new Set(array).
const a = new Set([1, 2, 3]); const b = new Set([3, 4]); a.union(b); // Set { 1, 2, 3, 4 } a.intersection(b); // Set { 3 } a.difference(b); // Set { 1, 2 }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
set.union(other) set.intersection(other) set.difference(other) set.symmetricDifference(other) set.isSubsetOf(other) set.isSupersetOf(other) set.isDisjointFrom(other)
| Method | Result |
|---|---|
union(other) |
A new set with every value that is in either set. |
intersection(other) |
A new set with the values that are in both sets. |
difference(other) |
A new set with the values of this set that are not in other. |
symmetricDifference(other) |
A new set with values that are in exactly one of the two sets. |
isSubsetOf(other) |
true if every value of this set is in other. |
isSupersetOf(other) |
true if every value of other is in this set. |
isDisjointFrom(other) |
true if the two sets have no value in common. |
| Name | Description |
|---|---|
other |
A set-like object: it must have a numeric size, a has() method and a keys() method. |
Return value: a new Set for the first four methods, a boolean for the last three.
Exceptions: a TypeError if other is not set-like (for example an array, which has no has() or size).
Usage notes
- They never mutate. Both operands are left unchanged and the result is a new set. To update a set in place, add to it from the result or loop over it.
- Arrays are rejected.
new Set([1]).union([2])throws aTypeError. Usea.union(new Set(array)). - Maps are accepted. A
Maphassize,has()andkeys(), so it works as the argument and its keys take part. - Result order is not a promise.
union()lists this set's values first, then the new values fromother, but the order produced byintersection()can depend on which set is smaller, so do not rely on it. differenceis not symmetric.a.difference(b)andb.difference(a)are different sets. UsesymmetricDifference()for "in one but not both".- The empty set is a subset of every set, and two empty sets are disjoint.
- Choosing a fallback. The spread and filter versions shown above give the same members and cost a little more memory. Libraries or a build step can also polyfill these methods.
Browser support
These methods are recent: current versions of the major browsers and of Node.js have them, but older ones do not. Check a compatibility table and keep a fallback if you support older environments.
Related
- Set
- Set.prototype.add(), has(), delete()
- Set iteration
- Lesson: Sets
- Lesson: Spread and Rest