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.

union()
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.

intersection()
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.

difference() and symmetricDifference()
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.

isSubsetOf() and isSupersetOf()
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));
📝 Note: The new Set methods (union, intersection, difference, etc.) are part of the TC39 proposal and are available in modern browsers and Node.js 22+. For older environments, use the manual alternatives with spread and filter.

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.

forEach and Spread
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]);
MethodReturnsDescription
union(other)New SetAll elements from both sets
intersection(other)New SetElements in both sets
difference(other)New SetElements in this but not other
symmetricDifference(other)New SetElements in either but not both
isSubsetOf(other)BooleanAll elements exist in other
isSupersetOf(other)BooleanContains all elements of other
isDisjointFrom(other)BooleanNo common elements
Exercise:
What does new Set([1,2,3]).intersection(new Set([2,3,4])) return?
Try it YourselfCtrl+Enter to run
Click Run to see the output here.