--- url: "https://web.dev/blog/set-methods" titulek: "The JavaScript Set methods are now part of Baseline | Blog | web.dev" h1: "The JavaScript Set methods are now part of Baseline Stay organized with collections Save and categorize content based on your preferences." popis: "JavaScript set methods like union and intersection are now interoperable, making set operations easier." jazyk: "en" kanonicka: "https://web.dev/blog/set-methods" drobecky: ["Blog", "The JavaScript Set methods are now part of Baseline"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:36:16" znaku: 3767 slov: 456 kodovani: "iso-8859-2" sha256: "ab966c47087295fcae60fceda7f73f0faf9bbe3d4047403568e848e56706151d" --- The JavaScript Set methods are now part of Baseline Stay organized with collections Save and categorize content based on your preferences. Adriana Jara Published: June 26, 2024 You can now use the JavaScript [`Set methods`](https://developer.mozilla.org/docs/Mozilla/Firefox/Releases/127#javascript) to perform set operations like [`intersection`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set/intersection), [`union`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set/union) and more. Sets are an essential data structure in any programming language. Now you can use JavaScript's built-in methods to perform set operations. Simplify your set operations using the following methods: `intersection()` [`intersection()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set/intersection) returns a new set containing elements in both this set and the given set. `const odds = new Set([1, 3, 5, 7, 9]); const squares = new Set([1, 4, 9]); console.log(odds.intersection(squares)); // Set(2) { 1, 9 }` `union()` [`union()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set/union) returns a new set containing all elements in this set and the given set. `const evens = new Set([2, 4, 6, 8]); const squares = new Set([1, 4, 9]); console.log(evens.union(squares)); // Set(6) { 2, 4, 6, 8, 1, 9 }` `difference()` [`difference()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set/difference) returns a new set containing elements in this set but not in the given set. `const odds = new Set([1, 3, 5, 7, 9]); const squares = new Set([1, 4, 9]); console.log(odds.difference(squares)); // Set(3) { 3, 5, 7 }` `symmetricDifference()` [`symmetricDifference()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set/symmetricDifference) returns a new set containing elements that are in either this set or the given set, but not in both. `const evens = new Set([2, 4, 6, 8]); const squares = new Set([1, 4, 9]); console.log(evens.symmetricDifference(squares)); // Set(5) { 2, 6, 8, 1, 9 }` `isSubsetOf()` [`isSubsetOf()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set/isSubsetOf) returns a boolean indicating if all elements of this set are in the given set. `const fours = new Set([4, 8, 12, 16]); const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]); console.log(fours.isSubsetOf(evens)); // true` `isSupersetOf()` [`isSupersetOf()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set/isSupersetOf) returns a boolean indicating if all elements of the given set are in this set. `const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]); const fours = new Set([4, 8, 12, 16]); console.log(evens.isSupersetOf(fours)); // true` `isDisjointFrom()` [`isDisjointFrom()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set/isDisjointFrom) Returns a boolean indicating if this set has no elements in common with the given set. `const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]); const squares = new Set([1, 4, 9, 16]); console.log(primes.isDisjointFrom(squares)); // true` Updating your code to use the built-in methods improves performance and reduces technical debt. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2024-06-26 UTC.