Sets come from math, and math has a standard set of ways to combine and compare them: which items are in both groups, which are in either, which are in one but not the other. JavaScript has 7 built-in methods for them. They reached browsers in 2024 and became an official part of the language in its 2025 edition.
For every example in this lesson:
const soccer = new Set(["Ava", "Ben", "Cal", "Dee"]);
const band = new Set(["Cal", "Dee", "Eli"]);
Everyone in soccer or band (or both), each listed once:
console.log([...soccer.union(band)]);
Students who do soccer and band:
console.log([...soccer.intersection(band)]);
Soccer players who are not in band. Order matters here:
console.log([...soccer.difference(band)]); // soccer only
console.log([...band.difference(soccer)]); // band only
Students in one activity but not both:
console.log([...soccer.symmetricDifference(band)]);
const starters = new Set(["Ava", "Cal"]);
const chess = new Set(["Fay", "Gus"]);
starters.isSubsetOf(soccer) // true every starter is on the soccer team
soccer.isSupersetOf(starters) // true soccer contains all the starters
soccer.isDisjointFrom(chess) // true no one in common
soccer.isDisjointFrom(band) // false Cal and Dee are in both
These methods need a Set (or a Map, whose keys are used) as the argument. An array does not work, even though new Set() happily accepts one:
const a = new Set([1, 2]);
console.log([...a.union(new Set([3]))]); // works
console.log([...a.union([3])]); // an array
The error mentions .size because the method looks for a size, and arrays have length instead. Wrap the array: a.union(new Set([3])).
| Method | Question | Returns |
|---|---|---|
| a.union(b) | In a or b? | new Set |
| a.intersection(b) | In both? | new Set |
| a.difference(b) | In a but not b? | new Set |
| a.symmetricDifference(b) | In exactly one? | new Set |
| a.isSubsetOf(b) | Is all of a inside b? | boolean |
| a.isSupersetOf(b) | Is all of b inside a? | boolean |
| a.isDisjointFrom(b) | Nothing in common? | boolean |
a.union(b).difference(c).
These methods arrived in 2024. Browsers from before then do not have them. The same results can be built with spread and filter, which is also a good way to understand what each one does:
const union = new Set([...soccer, ...band]);
const intersection = new Set([...soccer].filter(x => band.has(x)));
const difference = new Set([...soccer].filter(x => !band.has(x)));
const yesterday = new Set(["ava", "ben", "cal"]);
const today = new Set(["ben", "cal", "dee"]);
console.log("Joined:", [...today.difference(yesterday)]);
console.log("Left:", [...yesterday.difference(today)]);
console.log("Stayed:", [...today.intersection(yesterday)]);
A = new Set([1, 2, 3]) and B = new Set([2, 3, 4]). What is in A.difference(B)?
141 and 42 and 3difference keeps what is in A but not in B. C would be symmetricDifference, and D would be intersection.What happens?
const s = new Set(["a"]).union(["b"]);
TypeError: The .size property is NaN. The argument must be a Set (or Map). Use new Set(["a"]).union(new Set(["b"])).Which of these returns true or false instead of a new Set?
intersectionunionisSubsetOfsymmetricDifferenceis... methods compare Sets and answer yes or no. The other four build a new Set.new Set("aeiou") is disjoint from new Set("rhythm")symmetricDifference using only spread, filter, and has