How to perform intersection of two sets in JavaScript ? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 3 Likes Like Report 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 common from both sets. We can find the intersection of two sets in many ways: JavaScript Sets: Set can store any type of value whether primitive or objects.JavaScript filter() Method: Create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method. JavaScript set.has() Method: Check whether an element with a specified value exists in a Set or not. Using Sets & has() Method: A set is a collection of items that are unique i.e no element can be repeated. Set in ES6 are ordered: elements of the set can be iterated in the insertion order. A set can store any type of value whether primitive or objects. Example 1: In this example, we will see the intersection of sets by using the set.has() method. JavaScript function getIntersection(set1, set2) { const ans = new Set(); for (let i of set2) { if (set1.has(i)) { ans.add(i); } } return ans; } const set1 = new Set([1, 2, 3, 8, 11]); const set2 = new Set([1, 2, 5, 8]); const result = getIntersection(set1, set2); console.log(result); Output: Set(3) { 1, 2, 8 } Using the has() & filter() Method: We check if the element is present in set1 also then we are going to add that element to the new set. Example 2: In this example, we will see the intersection of sets by using the filter() method. We have also used the spread operator for separating the element. JavaScript let set1 = new Set([1, 2, 3]); let set2 = new Set([4, 3, 2]); let set = new Set([...set1].filter(x => set2.has(x))); console.log(set); Output: Set(2) { 2, 3 } Create Quiz Comment M mansigeekso9ii Follow 3 Improve M mansigeekso9ii Follow 3 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