How to get the union of two sets in JavaScript ? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes 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' } Create Quiz Comment M mansigeekso9ii Follow 1 Improve M mansigeekso9ii Follow 1 Improve Article Tags : JavaScript Web Technologies Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like