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.
1const friendA = new Set(['Inception', 'Interstellar', 'The Matrix']);2const friendB = new Set(['The Matrix', 'Avatar', 'Inception']);3
4const commonMovies = friendA.intersection(friendB);5console.log(commonMovies); // -> Set { 'Inception', 'The Matrix' }union()
The union() method returns a new set containing all unique elements from both sets.
1const recipeA = new Set(['flour', 'sugar', 'eggs']);2const recipeB = new Set(['sugar', 'milk', 'butter']);3
4const allIngredients = recipeA.union(recipeB);5console.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.
1const recipe = new Set(['flour', 'sugar', 'eggs', 'milk']);2const pantry = new Set(['flour', 'eggs', 'butter']);3
4const missingIngredients = recipe.difference(pantry);5console.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.
1const andresHobbies = new Set(['reading', 'cycling', 'hiking', 'painting']);2const felipeHobbies = new Set(['cycling', 'swimming', 'gaming', 'hiking']);3
4const uniqueHobbies = andresHobbies.symmetricDifference(felipeHobbies);5console.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.
1const shoppingList = new Set(['bread', 'butter', 'jam']);2const cart = new Set(['bread', 'butter', 'jam', 'milk']);3
4console.log(shoppingList.isSubsetOf(cart)); // -> trueisSupersetOf()
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.
1const inventory = new Set(['flour', 'sugar', 'eggs', 'milk', 'butter']);2const recipe = new Set(['flour', 'sugar', 'eggs']);3
4console.log(inventory.isSupersetOf(recipe)); // -> trueisDisjointFrom()
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.
1const groupA = new Set(['Alice', 'Bob', 'Charlie']);2const groupB = new Set(['Dave', 'Eve', 'Frank']);3
4console.log(groupA.isDisjointFrom(groupB)); // -> true