How to get the union of two sets in JavaScript ? Last Updated : 01 Aug, 2023 Summarize Suggest changes Share Like Article Like Report A union set is the combination of two elements. In mathematical terms, the union of two sets is shown by A ∪ B. It means all the elements in set A and set B should occur in a single array. In JavaScript, a union set means adding one set element to the other set. Set automatically take care of the uniqueness of elements. Below are the approaches through which we get the union of two sets in JavaScript: Using spread OperatorUsing add() MethodApproach 1: Using Spread OperatorSpread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array. Example: In this example, we will get the union of the two sets using the javascript spread operator. JavaScript // One set const num1 = new Set([1, 2, 3]); // Another set const num2 = new Set([4 , 2, 5]); const union = new Set([...num1, ...num2]); console.log(union); OutputSet(5) { 1, 2, 3, 4, 5 }Approach 2: Using add() Method It is used to append an element with a specified value in a set. It modifies the existing set by appending the new element but does not return a new set. Example: In this example, we will use add() to union the sets. JavaScript function showUnion(sA, sB) { const union = new Set(sA); for (const num of sB) { union.add(num); } return union; } const s1 = new Set(['1', '6', '8']); const s2 = new Set(['2', '3', '4']); console.log(showUnion(s1, s2)); OutputSet(6) { '1', '6', '8', '2', '3', '4' } Advertise with us Next Article How are elements ordered in a Set in JavaScript ? M mansigeekso9ii Follow Similar Reads Iterate over Set in JavaScript or JS We will iterate over set elements in JavaScript. A set is a collection of unique elements i.e. no element can appear more than once in a set.Below are the approaches to iterate over set elements in JavaScript:Approach 1: Using for...of loopThe forâ¦of loop iterates over the iterable objects (like Arr 3 min read How to get the union of two sets in JavaScript ? A union set is the combination of two elements. In mathematical terms, the union of two sets is shown by A ⪠B. It means all the elements in set A and set B should occur in a single array. In JavaScript, a union set means adding one set element to the other set. Set automatically take care of the un 2 min read How are elements ordered in a Set in JavaScript ? In JavaScript, a new object called Set was introduced in the ES6 version. The Set object is similar to Map but the major difference is that elements in the set are unique and repeated addition of the same type of elements is not possible. The set can store any type of value whether primitive or obje 2 min read How to sort a set in JavaScript ? Sorting a Set based on the values of its elements is a common need in JavaScript. While the built-in sort() function can't be directly applied to a Set, there's a workaround. Let's explore how we can achieve this: Instead of directly sorting the Set, we'll: Convert the Set into an array to make it s 3 min read How to Convert Array to Set in JavaScript? The Set constructor is used to convert the array into set in JavaScript. It is useful to clean the array and remove duplicate elements. Converting Array to Set means transforming the array elements into a Set object.Using Set ConstructorThe Set constructor in JavaScript directly converts an array in 2 min read How to map, reduce and filter a Set element using JavaScript ? The map(), reduce() and filter() are array functions that transform the array according to the applied function and return the updated array. They are used to write simple, short and clean codes for modifying an array instead of using the loops. map() method: It applies a given function on all the e 2 min read Difference between Set.clear() and Set.delete() Methods in JavaScript Both Set.clear() and Set.delete() methods modify the present Set by removing elements. But there are some fundamental differences between these two, which will be discussed in this article. Set.clear() Method: In this method, all the elements in the set get deleted. So, as a result, the set becomes 2 min read Set vs Map in JavaScript In JavaScript, Set and Map are two types of objects that are used for storing data in an ordered manner. Both these data structures are used to store distinct types of data inside the same object. In Maps, the data is stored as a key-value pair whereas in Set data is a single collection of values th 2 min read How to perform intersection of two sets in JavaScript ? An intersection of two sets means the element which occurs in both sets. In mathematical terms, the intersection of two sets is shown by A â© B. It means all the elements which is common in set A and set B should occur in a single array. In javascript, the intersection of two sets means getting the c 2 min read Get the Difference between Two Sets using JavaScript To get the difference between two sets in JavaScript, you need to identify elements present in the first set but not in the second. This involves iterating over the first set and filtering out elements that are also found in the second set, ensuring efficient comparison. We can get the difference be 2 min read Article Tags : JavaScript Web Technologies Like