New JavaScript Set methods | MDN Blog

New JavaScript Set methods | MDN Blog

The last of the new methods and concepts that we can explore are subset, superset, and ‘disjoint from’ methods.
By now, you’re familiar with the set operations that return new sets.
The subset, superset, and disjoint from methods don’t return new sets, but a Boolean value to indicate certain state or logical checks.

The first two we can look at are subset and superset.
Instead of highlighting list items, we have some statements or assertions like “Set one is a subset of set two”.
We then add some text (TRUE / FALSE) to the list item depending on the result of a check like if (set1.isSupersetOf(set2)):

if (set1.isSubsetOf(set2)) {
  oneSubTwo.textContent += " [TRUE]";
} else {
  oneSubTwo.textContent += " [FALSE]";
}

So with subset, we can check if all the items from a set appear in another set or not.
With superset, we’re doing the opposite, to see if a set has all items from another set, plus some additional items.

The last method for us to look at is isDisjointFrom() where we can find out if two sets have no common elements.
The first and second sets below are not disjoint from each other as they have one element (“C”) in common.
The third set is disjoint from the other two as it has no items in common with either set: