- Web Developer
- Posts
- 7 New JavaScript Set Methods

# 7 New JavaScript Set Methods

## Learn JavaScript Set Methods: Intersection, Union, Difference, SymmetricDifference, isSubsetOf, isSupersetOf, isDisjointFrom

TL;DR: JavaScript’s `Set`

data structure has added 7 new APIs that are very useful and can reduce the need for libraries like lodash. They are:

intersection() returns a new set with elements in both this set and the given set.

union() returns a new set with all elements in this set and the given set.

difference() returns a new set with elements in this set but not in the given set.

symmetricDifference() returns a new set with elements in either set, but not in both.

isSubsetOf() returns a boolean indicating if all elements of a set are in a specific set.

isSupersetOf() returns a boolean indicating if all elements of a set are in a specific set.

isDisjointFrom() returns a boolean indicating if this set has no elements in common with a specific set.

## 1. Set.prototype.intersection()

The `intersection()`

method of `Set`

instances takes a set and returns a new set containing elements in both this set and the given set.

**Venn diagram:**

**Example:**

## 2. Set.prototype.union()

The `union()`

method of `Set`

instances takes a set and returns a new set containing elements which are in either or both of this set and the given set.

**Venn diagram:**

**Example:**

## 3. Set.prototype.difference()

The `difference()`

method of `Set`

instances takes a set and returns a new set containing elements in this set but not in the given set.

**Venn diagram:**

**Example:**

## 4. Set.prototype.symmetricDifference()

The `symmetricDifference()`

method of `Set`

instances takes a set and returns a new set containing elements which are in either this set or the given set, but not in both.

**Venn diagram:**

**Example:**

## 5. Set.prototype.isSubsetOf()

The `isSubsetOf()`

method of `Set`

instances takes a set and returns a boolean indicating if all elements of this set are in the given set.

**Venn diagram:**

**Example:**

## 6. Set.prototype.isSupersetOf()

The `isSupersetOf()`

method of `Set`

instances takes a set and returns a boolean indicating if all elements of the given set are in this set.

**Venn diagram:**

**Example:**

## 7. Set.prototype.isDisjointFrom()

The `isDisjointFrom()`

method of `Set`

instances takes a set and returns a boolean indicating if this set has no elements in common with the given set.

**Venn diagram:**

**Example:**

*If you find my content helpful, please **consider subscribing**. I send a **weekly newsletter every Sunday** with the latest web development updates. Thanks for your support!*

## Reply