JavaScript Set isSupersetOf()
Syntax & Examples

Set.isSupersetOf() method

The isSupersetOf() method of the Set object in JavaScript takes another set as an argument and returns a boolean indicating if all elements of the given set are contained in the original set.


Syntax of Set.isSupersetOf()

The syntax of Set.isSupersetOf() method is:

isSupersetOf(other)

This isSupersetOf() method of Set takes a set and returns a boolean indicating if all elements of the given set are in this set.

Parameters

ParameterOptional/RequiredDescription
otherrequiredThe set to compare with the original set.

Return Type

Set.isSupersetOf() returns value of type boolean.



✐ Examples

1 Using isSupersetOf() to check if a set of numbers is a superset

In JavaScript, we can use the isSupersetOf() method to check if a set of numbers is a superset of another set.

For example,

  1. Create a new Set object setA with initial values 1, 2, and 3.
  2. Create another Set object setB with initial values 1 and 2.
  3. Use the isSupersetOf() method to check if setA is a superset of setB.
  4. Log the result to the console using console.log().

JavaScript Program

const setA = new Set([1, 2, 3]);
const setB = new Set([1, 2]);
const isSuperset = setA.isSupersetOf(setB);
console.log(isSuperset);

Output

true

2 Using isSupersetOf() to check if a set of strings is a superset

In JavaScript, we can use the isSupersetOf() method to check if a set of strings is a superset of another set.

For example,

  1. Create a new Set object setA with initial values 'apple', 'banana', and 'cherry'.
  2. Create another Set object setB with initial values 'apple' and 'banana'.
  3. Use the isSupersetOf() method to check if setA is a superset of setB.
  4. Log the result to the console using console.log().

JavaScript Program

const setA = new Set(['apple', 'banana', 'cherry']);
const setB = new Set(['apple', 'banana']);
const isSuperset = setA.isSupersetOf(setB);
console.log(isSuperset);

Output

true

3 Using isSupersetOf() to check if a set of objects is a superset

In JavaScript, we can use the isSupersetOf() method to check if a set of objects is a superset of another set of objects.

For example,

  1. Create a new Set object setA with initial objects representing different people.
  2. Create another Set object setB with a subset of those objects.
  3. Use the isSupersetOf() method to check if setA is a superset of setB.
  4. Log the result to the console using console.log().

JavaScript Program

const person1 = { name: 'John' };
const person2 = { name: 'Jane' };
const setA = new Set([person1, person2]);
const setB = new Set([person1]);
const isSuperset = setA.isSupersetOf(setB);
console.log(isSuperset);

Output

true

Summary

In this JavaScript tutorial, we learned about isSupersetOf() method of Set: the syntax and few working examples with output and detailed explanation for each example.