Set methods
The modern Set methods add built-in operations such as union(), intersection(), difference(), and subset checks. They make mathematical set logic easier to express directly.
Overview
The modern Set methods add built-in operations such as union(), intersection(), difference(), and subset checks. They make mathematical set logic easier to express directly.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 122 | 122 | 127 | 17 | 122 | 17 | |
| Built-in object | ||||||
| The intersection() method of Set instances takes a set and returns a new set containing elements in both this set and the given set. | 122 | 122 | 127 | 17 | 122 | 17 |
| The isDisjointFrom() method of Set instances takes a set and returns a boolean indicating if this set has no elements in common with the given set. | 122 | 122 | 127 | 17 | 122 | 17 |
| The isSubsetOf() method of Set instances takes a set and returns a boolean indicating if all elements of this set are in the given set. | 122 | 122 | 127 | 17 | 122 | 17 |
| The isSupersetOf() method of Set instances takes a set and returns a boolean indicating if all elements of the given set are in this set. | 122 | 122 | 127 | 17 | 122 | 17 |
| The symmetricDifference() method of Set instances takes a set and returns a new set containing elements which are in either this set or the given set, but not in both. | 122 | 122 | 127 | 17 | 122 | 17 |
| The union() method of Set instances takes a set and returns a new set containing elements which are in either or both of this set and the given set. | 122 | 122 | 127 | 17 | 122 | 17 |
Syntax
const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5, 6]);
a.union(b); // Set {1, 2, 3, 4, 5, 6}
a.intersection(b); // Set {3, 4}
a.difference(b); // Set {1, 2}
a.symmetricDifference(b); // Set {1, 2, 5, 6}
a.isSubsetOf(b); // false
a.isSupersetOf(b); // false Live demo
collectioncombine. Union (union)
2. collectioncombine unified, all. Duplicates element read..
commonpartial. Extract (intersection)
2. collectioncombine. both to includeelement(commonskil etc) extract..
Difference. Extract (difference)
oneside. collectioncombine to with, otherside to is element(not-aitemu etc) extract..
Use cases
Combining selections
Use union or difference to merge active filters, permissions, or selected IDs without duplicate logic.
Permission comparisons
Subset and superset checks make authorization and capability comparisons much clearer.
Cautions
- These APIs are newer than classic Set basics, so verify support targets or keep a polyfill strategy if needed.
- Set equality is still based on value identity rules, so object members are compared by reference.
Accessibility
- If set operations control visible selections, keep the selected state explicit in both visuals and assistive text.
Related links
Powered by web-features