The JavaScript Set
object just became a powerhouse of logic operations. With 7 new methods added in 2025, writing readable and efficient set-based logic is now a breeze! π―
Let's break it down π
π¦ Whatβs New in Set Logic?
JavaScript 2025 introduced 7 logical methods to the Set
prototype:
union()
difference()
intersection()
symmetricDifference()
isSubsetOf()
isSupersetOf()
isDisjointFrom()
β Supported since mid-2024 in all modern browsers:
Chrome 122, Edge 122, Firefox 127, Safari 17, Opera 108
π 1. union()
Combines all unique values from two sets.
const A = new Set(['a', 'b', 'c']);
const B = new Set(['b', 'c', 'd']);
const C = A.union(B);
console.log(C); // Set(4) { 'a', 'b', 'c', 'd' }
π€ 2. intersection()
Returns elements common to both sets.
const A = new Set(['a', 'b', 'c']);
const B = new Set(['b', 'c', 'd']);
const C = A.intersection(B);
console.log(C); // Set(2) { 'b', 'c' }
β 3. difference()
Returns elements in the first set that arenβt in the second.
const A = new Set(['a', 'b', 'c']);
const B = new Set(['b', 'c', 'd']);
const C = A.difference(B);
console.log(C); // Set(1) { 'a' }
π 4. symmetricDifference()
Returns elements that are in either set but not both.
const A = new Set(['a', 'b', 'c']);
const B = new Set(['b', 'c', 'd']);
const C = A.symmetricDifference(B);
console.log(C); // Set(2) { 'a', 'd' }
π§© 5. isSubsetOf()
Checks if all elements in
A
exist inB
.
const A = new Set(['b', 'c']);
const B = new Set(['a', 'b', 'c', 'd']);
console.log(A.isSubsetOf(B)); // true
π 6. isSupersetOf()
Checks if
A
contains all elements ofB
.
const A = new Set(['a', 'b', 'c', 'd']);
const B = new Set(['b', 'c']);
console.log(A.isSupersetOf(B)); // true
π« 7. isDisjointFrom()
Checks if
A
andB
share no common elements.
const A = new Set(['x', 'y']);
const B = new Set(['a', 'b']);
console.log(A.isDisjointFrom(B)); // true
π Why This Matters
Previously, you'd write verbose loops or use helper libraries like Lodash for set logic. Now, JavaScript handles it natively β cleaner, faster, and more readable.
π§ Final Thoughts
The new Set
methods finally give us the expressive power weβve needed in native JavaScript. Whether you're working with data structures, filtering sets, or building graph logic β this update is a total game-changer.
π¬ What will you use these for? Drop your thoughts in the comments!
π Full Reference
For all things Set
, check out: JavaScript Set Reference on MDN
Top comments (1)
this is great! it same a lot of time. I want to use Sets more