The isSubsetOf() method of the Set object in JavaScript takes another set as an argument and returns a boolean indicating if all elements of the original set are contained in the given set.
The syntax of Set.isSubsetOf() method is:
isSubsetOf(other)
This isSubsetOf() method of Set takes a set and returns a boolean indicating if all elements of this set are in the given set.
Parameter | Optional/Required | Description |
---|---|---|
other | required | The set to compare with the original set. |
Set.isSubsetOf() returns value of type boolean
.
In JavaScript, we can use the isSubsetOf()
method to check if a set of numbers is a subset of another set.
For example,
setA
with initial values 1 and 2.setB
with initial values 1, 2, and 3.isSubsetOf()
method to check if setA
is a subset of setB
.console.log()
.const setA = new Set([1, 2]);
const setB = new Set([1, 2, 3]);
const isSubset = setA.isSubsetOf(setB);
console.log(isSubset);
true
In JavaScript, we can use the isSubsetOf()
method to check if a set of strings is a subset of another set.
For example,
setA
with initial values 'apple' and 'banana'.setB
with initial values 'apple', 'banana', and 'cherry'.isSubsetOf()
method to check if setA
is a subset of setB
.console.log()
.const setA = new Set(['apple', 'banana']);
const setB = new Set(['apple', 'banana', 'cherry']);
const isSubset = setA.isSubsetOf(setB);
console.log(isSubset);
true
In JavaScript, we can use the isSubsetOf()
method to check if a set of objects is a subset of another set of objects.
For example,
setA
with initial objects representing different people.setB
with additional objects representing different people.isSubsetOf()
method to check if setA
is a subset of setB
.console.log()
.const person1 = { name: 'John' };
const person2 = { name: 'Jane' };
const setA = new Set([person1]);
const setB = new Set([person1, person2]);
const isSubset = setA.isSubsetOf(setB);
console.log(isSubset);
true
In this JavaScript tutorial, we learned about isSubsetOf() method of Set: the syntax and few working examples with output and detailed explanation for each example.