Dev Notes Blog

Exploring the New Set Methods in JavaScript

8th July 2024
Javascript
Javascript
Last updated:6th September 2025
2 Minutes
386 Words

JavaScript’s Set object has always been a powerful tool for handling collections of unique values. With the recent introduction of several new methods, working with sets has become even more intuitive and efficient.

Now, we’ll dive into the new Set methods: intersection(), union(), difference(), symmetricDifference(), isSubsetOf(), isSupersetOf(), and isDisjointFrom().

intersection()

The intersection() method returns a new set containing elements that are present in both the original set and a given set.

intersection.js
1
const friendA = new Set(['Inception', 'Interstellar', 'The Matrix']);
2
const friendB = new Set(['The Matrix', 'Avatar', 'Inception']);
3
4
const commonMovies = friendA.intersection(friendB);
5
console.log(commonMovies); // -> Set { 'Inception', 'The Matrix' }

union()

The union() method returns a new set containing all unique elements from both sets.

union.js
1
const recipeA = new Set(['flour', 'sugar', 'eggs']);
2
const recipeB = new Set(['sugar', 'milk', 'butter']);
3
4
const allIngredients = recipeA.union(recipeB);
5
console.log(allIngredients); // -> Set { 'flour', 'sugar', 'eggs', 'milk', 'butter' }

difference()

The difference() method returns a new set with elements that are in the original set but not in the given set.

difference.js
1
const recipe = new Set(['flour', 'sugar', 'eggs', 'milk']);
2
const pantry = new Set(['flour', 'eggs', 'butter']);
3
4
const missingIngredients = recipe.difference(pantry);
5
console.log(missingIngredients); // -> Set { 'sugar', 'milk' }

symmetricDifference()

The symmetricDifference() method returns a new set with elements that are in either of the sets but not in both.

symmetric-difference.js
1
const andresHobbies = new Set(['reading', 'cycling', 'hiking', 'painting']);
2
const felipeHobbies = new Set(['cycling', 'swimming', 'gaming', 'hiking']);
3
4
const uniqueHobbies = andresHobbies.symmetricDifference(felipeHobbies);
5
console.log(uniqueHobbies); // -> Set { 'reading', 'painting', 'swimming', 'gaming' }

isSubsetOf()

The isSubsetOf() method checks if all elements of the original set are present in the given set. It returns true if the original set is a subset of the given set, and false otherwise.

is-subset-of.js
1
const shoppingList = new Set(['bread', 'butter', 'jam']);
2
const cart = new Set(['bread', 'butter', 'jam', 'milk']);
3
4
console.log(shoppingList.isSubsetOf(cart)); // -> true

isSupersetOf()

The isSupersetOf() method checks if all elements of the given set are present in the original set. It returns true if the original set is a superset of the given set, and false otherwise.

is-superset-of.js
1
const inventory = new Set(['flour', 'sugar', 'eggs', 'milk', 'butter']);
2
const recipe = new Set(['flour', 'sugar', 'eggs']);
3
4
console.log(inventory.isSupersetOf(recipe)); // -> true

isDisjointFrom()

The isDisjointFrom() method checks if the original set has no elements in common with the given set. It returns true if the sets are disjoint, and false otherwise.

is-disjoint-from.js
1
const groupA = new Set(['Alice', 'Bob', 'Charlie']);
2
const groupB = new Set(['Dave', 'Eve', 'Frank']);
3
4
console.log(groupA.isDisjointFrom(groupB)); // -> true
Article title:Exploring the New Set Methods in JavaScript
Article author:Andrés Arias
Release time:8th July 2024