Set Methods
Set methods are seven built-in methods, added in ES2025, that combine two sets or compare them, using the operations from mathematics: union, intersection, difference and so on. Before ES2025 you had to write each one by hand with loops or filter. Now questions like "which students paid in both terms?" or "which grades have no payments yet?" are one method call each, and they read exactly like the question.
In One Minute
- Combining (return a new Set):
union,intersection,difference,symmetricDifference. - Comparing (return a boolean):
isSubsetOf,isSupersetOf,isDisjointFrom. - None of them change either set.
- The argument can be a Set, a Map, or any "set-like" object with
size,hasandkeys. A plain array is not accepted: wrap it innew Set(...). - Results keep the order of the set the method is called on, followed by new values from the other.
Method Table
| Method | Returns | Description |
|---|---|---|
a.union(b) | Set | It is used to get every value in a or b (or both). |
a.intersection(b) | Set | It is used to get only values that are in both a and b. |
a.difference(b) | Set | It is used to get values in a that are not in b. |
a.symmetricDifference(b) | Set | It is used to get values in exactly one of the two sets. |
a.isSubsetOf(b) | boolean | It is used to check whether every value of a is in b. |
a.isSupersetOf(b) | boolean | It is used to check whether every value of b is in a. |
a.isDisjointFrom(b) | boolean | It is used to check whether a and b share no values. |
Combining Sets
The Fee Tracker has the set of students who paid in the fall term and the set who paid in the spring:
const fall = new Set(["Navika", "Srihitha", "Advitha"]);
const spring = new Set(["Srihitha", "Advitha", "Zoe"]);
console.log(fall.union(spring));
console.log(fall.intersection(spring));
console.log(fall.difference(spring));
console.log(spring.difference(fall));
console.log(fall.symmetricDifference(spring));Set(4) {'Navika', 'Srihitha', 'Advitha', 'Zoe'}
Set(2) {'Srihitha', 'Advitha'}
Set(1) {'Navika'}
Set(1) {'Zoe'}
Set(2) {'Navika', 'Zoe'}| Question | Method | Answer |
|---|---|---|
| Who paid in either term? | fall.union(spring) | Navika, Srihitha, Advitha, Zoe |
| Who paid in both terms? | fall.intersection(spring) | Srihitha, Advitha |
| Who paid in fall but not spring? | fall.difference(spring) | Navika |
| Who paid in spring but not fall? | spring.difference(fall) | Zoe |
| Who paid in only one term? | fall.symmetricDifference(spring) | Navika, Zoe |
difference is the only one where the order matters: a.difference(b) and b.difference(a) give different answers. The others give the same values either way (possibly in a different order).
Comparing Sets
const required = new Set(["tuition", "books"]);
const paidItems = new Set(["tuition", "books", "lab"]);
const optional = new Set(["bus", "lunch"]);
console.log(required.isSubsetOf(paidItems));
console.log(paidItems.isSupersetOf(required));
console.log(required.isDisjointFrom(optional));
console.log(paidItems.isDisjointFrom(new Set(["lab", "bus"])));
console.log(new Set().isSubsetOf(required));true
true
true
false
trueExplanation: every required item was paid, so required is a subset of paidItems (and paidItems is a superset of required). Required and optional items share nothing, so they are disjoint. The empty set is a subset of every set, for the same reason every is true on an empty array.
Arrays Need Wrapping
The argument must be set-like: an object with a size property and has and keys methods. Sets and Maps qualify; arrays don't, because they have length and includes instead:
const grades = new Set([9, 10, 11]);
try {
grades.union([12]);
} catch (err) {
console.log(err.name);
}
console.log(grades.union(new Set([12])));
const feesByGrade = new Map([[9, 400], [12, 445]]);
console.log(grades.intersection(feesByGrade));TypeError
Set(4) {9, 10, 11, 12}
Set(1) {9}A Map works as the argument, and its keys are used. That makes "which of these grades have a fee defined?" a single call.
The Older Way
You'll see hand-written versions in code from before 2025. They do the same thing with spread and filter:
const fall = new Set(["Navika", "Srihitha"]);
const spring = new Set(["Srihitha", "Zoe"]);
const union = new Set([...fall, ...spring]);
const intersection = new Set([...fall].filter((x) => spring.has(x)));
const difference = new Set([...fall].filter((x) => !spring.has(x)));
console.log(union, intersection, difference);Set(3) {'Navika', 'Srihitha', 'Zoe'} Set(1) {'Srihitha'} Set(1) {'Navika'}Set methods run in Node.js 26 and in all current major browsers (they have been Baseline since 2024), so as of November 2026 they are safe to use without a fallback.
In Practice
The Fee Tracker's term report uses set operations to find returning students, new students, students who left, and whether every required item was paid:
const fallPaid = new Set(["Navika", "Srihitha", "Advitha"]);
const springPaid = new Set(["Srihitha", "Advitha", "Zoe"]);
const REQUIRED = new Set(["tuition", "books"]);
const itemsPaidBy = {
Srihitha: new Set(["tuition", "books", "lab"]),
Advitha: new Set(["tuition"]),
Zoe: new Set(["tuition", "books"]),
};
console.log("Returning:", [...fallPaid.intersection(springPaid)].join(", "));
console.log("New:", [...springPaid.difference(fallPaid)].join(", "));
console.log("Left:", [...fallPaid.difference(springPaid)].join(", "));
for (const name of springPaid) {
const missing = REQUIRED.difference(itemsPaidBy[name]);
console.log(`${name}: ${missing.size === 0 ? "complete" : "missing " + [...missing].join(", ")}`);
}Returning: Srihitha, Advitha
New: Zoe
Left: Navika
Srihitha: complete
Advitha: missing books
Zoe: completeCommon Mistakes
Passing an array. set.union([1, 2]) throws a TypeError. Wrap it: set.union(new Set([1, 2])).
Expecting the original to change. The combining methods return new sets. Store the result.
Mixing up the direction of difference. a.difference(b) keeps values of a. Read it as "a minus b".
Comparing sets with ===. Two sets with the same values are different objects. Two sets are equal when each is a subset of the other: a.size === b.size && a.isSubsetOf(b).
Practice
Challenge: Write sameMembers(a, b) that returns true when two sets contain exactly the same values, using the new methods, and test it.
Show solution
function sameMembers(a, b) {
return a.size === b.size && a.isSubsetOf(b);
}
console.log(sameMembers(new Set([9, 10]), new Set([10, 9])));
console.log(sameMembers(new Set([9, 10]), new Set([9, 11])));
console.log(sameMembers(new Set([9]), new Set([9, 10])));true
false
falseKey Takeaways
| Question | Code |
|---|---|
| In either | a.union(b) |
| In both | a.intersection(b) |
| In a only | a.difference(b) |
| In exactly one | a.symmetricDifference(b) |
| Is a inside b? | a.isSubsetOf(b) |
| Nothing shared? | a.isDisjointFrom(b) |
Was this tutorial helpful?