Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Set
  6. isSupersetOf()

Set.prototype.isSupersetOf()

Baseline 2024
Newly available

Since June 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

TheisSupersetOf() method ofSet instances takes a set and returns a boolean indicating if all elements of the given set are in this set.

Syntax

js
isSupersetOf(other)

Parameters

other

ASet object, orset-like object.

Return value

true if all elements in theother set are also in this set, andfalse otherwise.

Description

In mathematical notation,superset is defined as:

ABxB,xAA\supseteq B \Leftrightarrow \forall x\in B,\,x\in A

And using Venn diagram:

A Venn diagram with two circles. A is a superset of B because B is completely contained in A.

Note:Thesuperset relationship is notproper superset, which meansisSupersetOf() returnstrue ifthis andother contain the same elements.

isSupersetOf() 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 fewer elements inthis thanother.size, then it directly returnsfalse.
  • Otherwise, it iterates overother by calling itskeys() method, and if any element inother is not present inthis, it returnsfalse (and closes thekeys() iterator by calling itsreturn() method). Otherwise, it returnstrue.

Examples

Using isSupersetOf()

The set of even numbers (<20) is a superset of multiples of 4 (<20):

js
const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);const fours = new Set([4, 8, 12, 16]);console.log(evens.isSupersetOf(fours)); // true

The set of all odd numbers (<20) is not a superset of prime numbers (<20), because 2 is prime but not odd:

js
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(odds.isSupersetOf(primes)); // false

Equivalent sets are supersets of each other:

js
const set1 = new Set([1, 2, 3]);const set2 = new Set([1, 2, 3]);console.log(set1.isSupersetOf(set2)); // trueconsole.log(set2.isSupersetOf(set1)); // true

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-set.prototype.issupersetof

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp