DEV Community

Cover image for πŸ”₯ JavaScript Set Logic Just Leveled Up (2025 Update)
Raj Aryan
Raj Aryan

Posted on

πŸ”₯ JavaScript Set Logic Just Leveled Up (2025 Update)

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()
Enter fullscreen mode Exit fullscreen mode

βœ… 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' }
Enter fullscreen mode Exit fullscreen mode

🀝 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' }
Enter fullscreen mode Exit fullscreen mode

βž– 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' }
Enter fullscreen mode Exit fullscreen mode

πŸ” 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' }
Enter fullscreen mode Exit fullscreen mode

🧩 5. isSubsetOf()

Checks if all elements in A exist in B.

const A = new Set(['b', 'c']);
const B = new Set(['a', 'b', 'c', 'd']);
console.log(A.isSubsetOf(B)); // true
Enter fullscreen mode Exit fullscreen mode

πŸ“š 6. isSupersetOf()

Checks if A contains all elements of B.

const A = new Set(['a', 'b', 'c', 'd']);
const B = new Set(['b', 'c']);
console.log(A.isSupersetOf(B)); // true
Enter fullscreen mode Exit fullscreen mode

🚫 7. isDisjointFrom()

Checks if A and B share no common elements.

const A = new Set(['x', 'y']);
const B = new Set(['a', 'b']);
console.log(A.isDisjointFrom(B)); // true
Enter fullscreen mode Exit fullscreen mode

πŸ“˜ 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)

Collapse
Β 
93025869dd7fd096fa2 profile image
falsy β€’

this is great! it same a lot of time. I want to use Sets more