JavaScript Set Methods
ES2025 introduced new Set methods for performing mathematical set operations like union, intersection, and difference. These methods return new Sets and do not modify the originals.
union()
The union() method returns a new Set containing all elements from both sets. It combines elements without duplicates.
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);
const unionSet = setA.union(setB);
console.log("Union:", [...unionSet]);
// Original sets unchanged
console.log("setA:", [...setA]);
console.log("setB:", [...setB]);
// Manual union (for older environments)
const manualUnion = new Set([...setA, ...setB]);
console.log("Manual union:", [...manualUnion]);intersection()
The intersection() method returns a new Set containing only elements that exist in both sets.
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);
const inter = setA.intersection(setB);
console.log("Intersection:", [...inter]);
// Manual intersection
const manualInter = new Set([...setA].filter(x => setB.has(x)));
console.log("Manual intersection:", [...manualInter]);
// No common elements
const setC = new Set([10, 20]);
console.log("Empty intersection:", [...setA.intersection(setC)]);difference() and symmetricDifference()
difference() returns elements in the first set that are not in the second. symmetricDifference() returns elements that are in either set, but not in both.
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);
// A - B (elements in A but not in B)
const diff = setA.difference(setB);
console.log("A - B:", [...diff]);
// B - A (elements in B but not in A)
const diff2 = setB.difference(setA);
console.log("B - A:", [...diff2]);
// Symmetric difference (in either, not both)
const symDiff = setA.symmetricDifference(setB);
console.log("Symmetric diff:", [...symDiff]);
// Manual difference
const manualDiff = new Set([...setA].filter(x => !setB.has(x)));
console.log("Manual A - B:", [...manualDiff]);isSubsetOf() and isSupersetOf()
isSubsetOf() checks if all elements of a set exist in another set. isSupersetOf() checks if a set contains all elements of another set.
const setA = new Set([1, 2, 3, 4, 5]);
const setB = new Set([2, 3]);
const setC = new Set([2, 3, 99]);
// isSubsetOf
console.log("B subset of A:", setB.isSubsetOf(setA));
console.log("C subset of A:", setC.isSubsetOf(setA));
// isSupersetOf
console.log("A superset of B:", setA.isSupersetOf(setB));
console.log("A superset of C:", setA.isSupersetOf(setC));
// isDisjointFrom - no common elements
const setD = new Set([10, 20]);
console.log("A disjoint from D:", setA.isDisjointFrom(setD));
console.log("A disjoint from B:", setA.isDisjointFrom(setB));forEach() and Spread Operator
Sets support forEach() for iteration and the spread operator for converting to arrays. These are the most common ways to work with Set data.
const fruits = new Set(["Apple", "Banana", "Cherry"]);
// forEach
fruits.forEach((value) => {
console.log("Fruit:", value);
});
// Spread to array
const arr = [...fruits];
console.log("Array:", arr);
// Use array methods on Set data
const mapped = [...fruits].map(f => f.toUpperCase());
console.log("Uppercase:", mapped);
const filtered = [...fruits].filter(f => f.length > 5);
console.log("Long names:", filtered);
// Spread to combine sets
const set1 = new Set([1, 2, 3]);
const set2 = new Set([3, 4, 5]);
const combined = new Set([...set1, ...set2]);
console.log("Combined:", [...combined]);| Method | Returns | Description |
|---|---|---|
| union(other) | New Set | All elements from both sets |
| intersection(other) | New Set | Elements in both sets |
| difference(other) | New Set | Elements in this but not other |
| symmetricDifference(other) | New Set | Elements in either but not both |
| isSubsetOf(other) | Boolean | All elements exist in other |
| isSupersetOf(other) | Boolean | Contains all elements of other |
| isDisjointFrom(other) | Boolean | No common elements |