Set.prototype.isSubsetOf()
Returns true if every element of this set is also present in the given set.
isSubsetOf(other)- Added in JavaScript ES2025
Parameters
| Parameter | Type | Description |
|---|---|---|
$other | Set | set-like object | A Set, or any set-like object: one with a numeric size property and callable has() and keys() methods. Only its has() method is consulted. |
Return value
booleantrue if all elements in this set are also in the other set, and false otherwise.
Errors
Throws a TypeError if other is not set-like — missing a non-negative numeric size, or missing callable has() and keys() methods.
Changelog
- PHP ES2025Added with the other Set composition methods; Baseline newly available since June 2024.
isSubsetOf() replaces the familiar [...a].every((v) => b.has(v)) loop with a single call, and it short-circuits: if this.size is greater than other.size it returns false without inspecting a single element.
When to reach for it
Reach for it on any "do I have everything I need?" check — are the requested scopes all covered by the granted scopes, are the columns a query touches all present in the table, are the tags on a post all drawn from the allowed vocabulary. It states the intent in the code, which a manual loop does not.
What trips people up
The relation is not proper subset. Two sets with exactly the same elements are subsets of each other, so a.isSubsetOf(b) is true when they are equal. For a strict subset, add a size check: a.size < b.size && a.isSubsetOf(b).
The receiver must be a real Set; only the argument may be set-like. And note which side is inspected — this method calls other.has() for each of its own elements, while isSupersetOf() iterates other.keys() instead. For a hand-written set-like whose has() and keys() disagree, the two methods can give inconsistent answers.
Example
When to use it
- Check that every scope a request needs is covered by the token's granted scopes
- Validate that all tags on a post come from an allowed vocabulary
- Assert in a test that the observed events are a subset of the expected ones
- Confirm a query's columns all exist in a table's column set
Frequently asked questions
Does isSubsetOf return true for identical sets?
Yes — the test is for a subset, not a proper subset, so equal sets are subsets of each other. Write a.size < b.size && a.isSubsetOf(b) if you need strictness.
Can I pass an array to isSubsetOf?
No, the argument must be set-like, and arrays expose length rather than size, so passing one throws a TypeError. Convert it with new Set(arr) first.
Is isSubsetOf faster than checking every element with has()?
Usually, because it returns false immediately when this set is larger than the other and otherwise stops at the first element that is missing. The worst case is still one has() call per element of the receiver.
More examples
Reporting what is missing
isSubsetOf() gives the yes/no answer cheaply; difference() is only computed on the failing branch to name the offending fields. The output lists the single required key that was not supplied.
const required = new Set(["name", "email", "country"]);
const provided = new Set(["name", "email"]);
if (!required.isSubsetOf(provided)) {
console.log("missing:", [...required.difference(provided)]); // missing: ["country"]
} else {
console.log("complete");
}A Map as the argument
A Map is set-like, so it can be tested against directly and is matched on its keys. The first check passes because both wanted keys are cached; the second fails on the key "z", which the map does not hold.
const wanted = new Set(["a", "c"]);
const cache = new Map([
["a", 1],
["b", 2],
["c", 3],
]);
console.log(wanted.isSubsetOf(cache)); // true
console.log(new Set(["a", "z"]).isSubsetOf(cache)); // falseObjects are matched by reference
Membership uses SameValueZero, which compares object references rather than contents. A freshly built { id: 1 } is a different reference from a, so the second check reports false even though the objects look identical.
const a = { id: 1 };
const b = { id: 2 };
console.log(new Set([a]).isSubsetOf(new Set([a, b]))); // true
console.log(new Set([{ id: 1 }]).isSubsetOf(new Set([a, b]))); // false
Discussion