Set.prototype.isSupersetOf()
Returns true if this set contains every element of the given set.
isSupersetOf(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. Its keys() iterator is what gets walked. |
Return value
booleantrue if all elements in the other set are also in this 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.
isSupersetOf() is the mirror of isSubsetOf(): a.isSupersetOf(b) asks whether a covers everything in b. Pick whichever direction puts the meaningful collection first — allowed.isSupersetOf(requested) reads as "the allowlist covers this request", which is often clearer than flipping it around.
How it walks the arguments
The implementation short-circuits to false when this.size is smaller than other.size. Otherwise it iterates other by calling its keys() method and checks each value against this set, closing the iterator as soon as it finds one that is missing. That is the opposite of isSubsetOf(), which calls other.has() — worth knowing when you hand-roll a set-like object, because a lazy or expensive keys() is what gets exercised here.
What trips people up
Like the subset test, this is not proper superset: equal sets return true in both directions. And only the receiver has to be a real Set; the argument may be any set-like object, including a Map, which contributes its keys.
Example
When to use it
- Check that an allowlist covers every scope a request asks for
- Confirm a cache already holds all the ids a batch render needs
- Assert that a rendered menu contains every item a role is entitled to
- Verify an installed-plugins set covers a theme's declared dependencies
Frequently asked questions
What is the difference between isSupersetOf and isSubsetOf?
They are the same relation read in opposite directions: a.isSupersetOf(b) is equivalent to b.isSubsetOf(a) for real Sets. They differ internally in that isSupersetOf() iterates other.keys() while isSubsetOf() calls other.has().
Does isSupersetOf return true when both sets are equal?
Yes, because it tests for a superset rather than a proper superset. Combine it with a.size > b.size for strictness.
Can the argument be a Map or a custom object?
Yes — any set-like object works, meaning one with a numeric size and callable has() and keys() methods; a Map is compared on its keys. The receiver, however, must be an actual Set instance.
More examples
Guarding a batch operation
The superset test decides whether any network work is needed at all. Because one id is uncached, the else branch runs and difference() names exactly the id that must be fetched.
const cached = new Set(["u1", "u2", "u3"]);
const page = new Set(["u2", "u4"]);
if (cached.isSupersetOf(page)) {
console.log("render from cache");
} else {
console.log("fetch:", [...page.difference(cached)]); // fetch: ["u4"]
}Comparing against a Map's keys
The Map is walked through its keys() iterator, so only its keys are compared and the values are ignored. The schema covers id and email, but has no nickname column, so the second check is false.
const columns = new Set(["id", "name", "email", "created_at"]);
const row = new Map([
["id", 7],
["email", "[email protected]"],
]);
console.log(columns.isSupersetOf(row)); // true
console.log(columns.isSupersetOf(new Map([["nickname", "x"]]))); // falseProper superset requires a size check
The first line shows that equal sets satisfy isSupersetOf(). Adding a size comparison turns it into a proper-superset test, which only becomes true once a genuinely holds something extra.
const a = new Set([1, 2, 3]);
const b = new Set([1, 2, 3]);
console.log(a.isSupersetOf(b)); // true
console.log(a.size > b.size && a.isSupersetOf(b)); // false — not a proper superset
a.add(4);
console.log(a.size > b.size && a.isSupersetOf(b)); // true
Discussion