New JavaScript Set Methods: union, intersection and difference

A
Author
22 Jul, 2026 1 min read 1.03K views

JavaScript has had Set since ES2015, but combining sets meant converting to arrays and filtering. ES2025 added the classic set operations as methods.

The operations

const frontend = new Set(['asha', 'ravi', 'meera']);
const oncall   = new Set(['ravi', 'kiran']);

frontend.union(oncall);               // Set {'asha', 'ravi', 'meera', 'kiran'}
frontend.intersection(oncall);        // Set {'ravi'}
frontend.difference(oncall);          // Set {'asha', 'meera'}
frontend.symmetricDifference(oncall); // Set {'asha', 'meera', 'kiran'}

Comparisons

const required = new Set(['read', 'write']);
const granted  = new Set(['read', 'write', 'delete']);

required.isSubsetOf(granted);      // true: user has every required permission
granted.isSupersetOf(required);    // true
required.isDisjointFrom(new Set(['admin'])); // true: nothing in common

Real-world examples

// Which selected rows are new since last save?
const added = new Set(selectedIds).difference(new Set(savedIds));
const removed = new Set(savedIds).difference(new Set(selectedIds));

// Tags shared by two articles
const shared = new Set(a.tags).intersection(new Set(b.tags));

// Feature access check
const canPublish = new Set(['write', 'publish']).isSubsetOf(user.permissions);

Details

  • All methods return new sets; the originals aren't changed.
  • The argument can be any "set-like" object with size, has() and keys(), including a Map.
  • Use [...set] or Array.from(set) when you need an array back.
  • Available in current versions of all major browsers and Node.js 22+.
Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.