Set.prototype.isSubsetOf()
Baseline 2024Newly available
Since June 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
TheisSubsetOf() method ofSet instances takes a set and returns a boolean indicating if all elements of this set are in the given set.
In this article
Syntax
isSubsetOf(other)Parameters
Return value
true if all elements in this set are also in theother set, andfalse otherwise.
Description
In mathematical notation,subset is defined as:
And using Venn diagram:
Note:Thesubset relationship is notproper subset, which meansisSubsetOf() returnstrue ifthis andother contain the same elements.
isSubsetOf() acceptsset-like objects as theother parameter. It requiresthis to be an actualSet instance, because it directly retrieves the underlying data stored inthis without invoking any user code. Then, its behavior depends on the sizes ofthis andother:
- If there are more elements in
thisthanother.size, then it directly returnsfalse. - Otherwise, it iterates over the elements in
this, and returnsfalseif any elementeinthiscausesother.has(e)to return afalsy value. Otherwise, it returnstrue.
Examples
>Using isSubsetOf()
The set of multiples of 4 (<20) is a subset of even numbers (<20):
const fours = new Set([4, 8, 12, 16]);const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);console.log(fours.isSubsetOf(evens)); // trueThe set of prime numbers (<20) is not a subset of all odd numbers (<20), because 2 is prime but not odd:
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);const odds = new Set([3, 5, 7, 9, 11, 13, 15, 17, 19]);console.log(primes.isSubsetOf(odds)); // falseEquivalent sets are subsets of each other:
const set1 = new Set([1, 2, 3]);const set2 = new Set([1, 2, 3]);console.log(set1.isSubsetOf(set2)); // trueconsole.log(set2.isSubsetOf(set1)); // trueSpecifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-set.prototype.issubsetof> |