Sub topic 7
Comparing Sets: isSubsetOf, isSupersetOf, isDisjointFrom
The composition methods covered earlier (union, intersection, difference) each build a new Set. Sometimes you only need a yes-or-no answer about how two sets relate. ES2025 adds three predicate methods for that. Each one returns a boolean and creates no new Set.
This chapter assumes you know how to create a Set, call has, and read the result of intersection and difference.
The Three Questions
Each predicate answers a single question about the receiver (the set you call the method on) and the argument.
| Method | Question it answers | True when |
|---|---|---|
| `a.isSubsetOf(b)` | Is all of `a` inside `b`? | Every element of `a` is in `b` |
| `a.isSupersetOf(b)` | Does `a` contain all of `b`? | Every element of `b` is in `a` |
| `a.isDisjointFrom(b)` | Do `a` and `b` share nothing? | No element is in both |
const required = new Set(["read", "write"]);
const granted = new Set(["read", "write", "delete"]);
const banned = new Set(["admin"]);
console.log(required.isSubsetOf(granted)); // true
console.log(granted.isSupersetOf(required)); // true
console.log(granted.isDisjointFrom(banned)); // trueThe first two methods are mirror images. a.isSubsetOf(b) always gives the same answer as b.isSupersetOf(a). Choose the one that reads naturally at the call site. "Are the required permissions a subset of the granted ones?" and "Does the user's grant cover the requirement?" ask the same thing.
How They Relate to the Composition Methods
Each predicate has an equivalent built from methods you already know. These are useful for checking your intuition:
a.isSubsetOf(b)is the same asa.difference(b).size === 0.a.isSupersetOf(b)is the same asb.difference(a).size === 0.a.isDisjointFrom(b)is the same asa.intersection(b).size === 0.
The predicates are better choices in real code. They don't allocate a new Set, and they can stop early. isSubsetOf returns false at the first element of a that is missing from b. If a.size is larger than b.size, it returns false without looking at any element at all. isDisjointFrom stops at the first shared element.
Edge Cases That Trip People Up
The empty set
An empty Set is a subset of every set, and every set is a superset of the empty set. An empty set is also disjoint from everything, including another empty set. None of these checks can find a counterexample, so they return true.
const empty = new Set();
const nums = new Set([1, 2]);
console.log(empty.isSubsetOf(nums)); // true
console.log(nums.isSupersetOf(empty)); // true
console.log(empty.isDisjointFrom(empty)); // trueThis matters in permission checks. If the set of required permissions is empty, required.isSubsetOf(granted) passes for every user. That is usually the correct result, but make sure you meant it.
A set compared with itself
A set is a subset and a superset of itself. Mathematically, "subset" here includes equality. There is no built-in isProperSubsetOf. To check for a strict subset, add a size comparison:
function isProperSubset(a, b) {
return a.size < b.size && a.isSubsetOf(b);
}
const small = new Set([1, 2]);
const big = new Set([1, 2, 3]);
console.log(isProperSubset(small, big)); // true
console.log(isProperSubset(big, big)); // falseTesting equality
JavaScript has no Set.prototype.equals. Two sets hold the same elements when they have the same size and one is a subset of the other:
function setsEqual(a, b) {
return a.size === b.size && a.isSubsetOf(b);
}
console.log(setsEqual(new Set([1, 2]), new Set([2, 1]))); // true
console.log(setsEqual(new Set([1, 2]), new Set([1, 3]))); // falseOrder doesn't matter in this check. a === b would only test whether both variables refer to the same Set object.
Exercises
Exercise
Given const a = new Set([1, 2]); const b = new Set([2, 3]);, what do a.isSubsetOf(b), a.isSupersetOf(b), and a.isDisjointFrom(b) return?
Show answer
All three return false. 1 is not in b, so a is not a subset. 3 is not in a, so a is not a superset. Both sets contain 2, so they are not disjoint.
Exercise
A feature requires the permissions in needed. A user holds the permissions in held. Write one expression that is true when the user may use the feature. Then say what it returns when needed is empty.
Show answer
needed.isSubsetOf(held), or the equivalent held.isSupersetOf(needed). When needed is empty, it returns true for every user, because the empty set is a subset of every set.
Exercise
Why does new Set([1, 2]).isSubsetOf(new Set([1, 2])) return true, and how do you check for a strict subset instead?
Show answer
"Subset" includes equality, so every set is a subset of itself. For a strict subset, also require a smaller size: a.size < b.size && a.isSubsetOf(b).