Set Methods

Updated · 6 min read
mediumES2026javascriptsetset-methodses2025

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, has and keys. A plain array is not accepted: wrap it in new Set(...).
  • Results keep the order of the set the method is called on, followed by new values from the other.

Method Table

MethodReturnsDescription
a.union(b)SetIt is used to get every value in a or b (or both).
a.intersection(b)SetIt is used to get only values that are in both a and b.
a.difference(b)SetIt is used to get values in a that are not in b.
a.symmetricDifference(b)SetIt is used to get values in exactly one of the two sets.
a.isSubsetOf(b)booleanIt is used to check whether every value of a is in b.
a.isSupersetOf(b)booleanIt is used to check whether every value of b is in a.
a.isDisjointFrom(b)booleanIt 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:

combine.js▶ Try it
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));
Output
Set(4) {'Navika', 'Srihitha', 'Advitha', 'Zoe'}
Set(2) {'Srihitha', 'Advitha'}
Set(1) {'Navika'}
Set(1) {'Zoe'}
Set(2) {'Navika', 'Zoe'}
QuestionMethodAnswer
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

compare.js▶ Try it
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));
Output
true
true
true
false
true

Explanation: 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:

array-arg.js▶ Try it
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));
Output
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:

older.js▶ Try it
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);
Output
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:

term-report.js▶ Try it
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(", ")}`);
}
Output
Returning: Srihitha, Advitha
New: Zoe
Left: Navika
Srihitha: complete
Advitha: missing books
Zoe: complete

Common 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
same-members.js▶ Try it
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])));
Output
true
false
false

Quick check: Set methods

3 questions
1What is new Set([1, 2, 3]).intersection(new Set([2, 3, 4]))?
2Which method answers "which values are in a but not in b"?
3What happens with new Set([1]).union([2])?

Key Takeaways

QuestionCode
In eithera.union(b)
In botha.intersection(b)
In a onlya.difference(b)
In exactly onea.symmetricDifference(b)
Is a inside b?a.isSubsetOf(b)
Nothing shared?a.isDisjointFrom(b)

Was this tutorial helpful?

Lesson 47 of 139