Sub topic 12
Putting Set Methods to Work
This chapter assumes you know the methods covered earlier in the tutorial: union, intersection, difference, symmetricDifference, and the predicates isSubsetOf, isSupersetOf, and isDisjointFrom. It also assumes you are comfortable with Array.prototype.filter and map. The examples run in any runtime that supports ES2025 Set methods, such as Node.js 22 or later or a current browser.
The skill this chapter builds is turning a plain-language question into the right Set method.
Matching Questions to Methods
Most real tasks start as a question about two collections. The table below maps common questions to the method that answers them.
| Question | Method | Returns |
|---|---|---|
| What is in either collection? | `a.union(b)` | Set |
| What do both share? | `a.intersection(b)` | Set |
| What does `a` have that `b` lacks? | `a.difference(b)` | Set |
| What is in exactly one? | `a.symmetricDifference(b)` | Set |
| Does `b` contain all of `a`? | `a.isSubsetOf(b)` | boolean |
| Do they share nothing? | `a.isDisjointFrom(b)` | boolean |
Checking Permissions
An access check needs two answers: whether the user is allowed in, and what is missing if they are not. A predicate gives the first answer and difference gives the second.
function checkAccess(required, granted) {
if (required.isSubsetOf(granted)) {
return { ok: true, missing: [] };
}
return { ok: false, missing: [...required.difference(granted)] };
}
const required = new Set(["read", "write", "share"]);
console.log(checkAccess(required, new Set(["read", "write", "share", "delete"])));
// { ok: true, missing: [] }
console.log(checkAccess(required, new Set(["read"])));
// { ok: false, missing: [ 'write', 'share' ] }The predicate exits early and builds no new Set, so the common path, where the user has access, costs less.
Syncing ID Lists
Suppose you have the IDs a server held before a sync and the IDs it holds after. Each question about the change is one method call. The argument order matters for difference.
const before = new Set([101, 102, 103]);
const after = new Set([102, 103, 104, 105]);
const added = after.difference(before);
const removed = before.difference(after);
const kept = before.intersection(after);
const changed = before.symmetricDifference(after);
console.log([...added]); // [104, 105]
console.log([...removed]); // [101]
console.log([...kept]); // [102, 103]
console.log([...changed]); // [101, 104, 105]changed holds the same elements as removed and added combined. Use symmetricDifference when you only need to know what changed. Use the two difference calls when you need to treat additions and removals differently.
const oldUsers = [{ id: 1 }, { id: 2 }];
const newUsers = [{ id: 2 }, { id: 3 }];
const oldIds = new Set(oldUsers.map((u) => u.id));
const newIds = new Set(newUsers.map((u) => u.id));
console.log([...newIds.difference(oldIds)]); // [3]Filtering by Tags
Tag filters usually come in three modes: match all tags, match any tag, or match none. Each mode maps to one predicate.
const posts = [
{ title: "Intro to Sets", tags: new Set(["js", "beginner"]) },
{ title: "Advanced Maps", tags: new Set(["js", "advanced"]) },
{ title: "CSS Grid", tags: new Set(["css", "layout"]) },
];
const wanted = new Set(["js", "advanced"]);
const titles = (list) => list.map((p) => p.title);
const matchAll = posts.filter((p) => wanted.isSubsetOf(p.tags));
const matchAny = posts.filter((p) => !wanted.isDisjointFrom(p.tags));
const matchNone = posts.filter((p) => wanted.isDisjointFrom(p.tags));
console.log(titles(matchAll)); // ["Advanced Maps"]
console.log(titles(matchAny)); // ["Intro to Sets", "Advanced Maps"]
console.log(titles(matchNone)); // ["CSS Grid"]To build a tag cloud from every post, fold the tag Sets together with union:
const allTags = posts.reduce((acc, p) => acc.union(p.tags), new Set());
console.log([...allTags]); // ["js", "beginner", "advanced", "css", "layout"]Exercise
A feature flag service stores the flags enabled for staging and for production as Sets of strings. Write expressions that answer two questions: which flags are on in staging but not yet in production, and whether production has any flag that staging lacks.
Show answer
Use staging.difference(production) for the flags waiting to be promoted. Use !production.isSubsetOf(staging) to check whether production has extra flags. That expression is true when at least one production flag is missing from staging. For the list of those flags, use production.difference(staging).
Exercise
Given const a = new Set(["x", "y"]); const b = new Set(["y", "z"]);, what does [...a.difference(b).union(b.difference(a))] print, and which single method call returns the same elements in the same order?
Show answer
It prints ["x", "z"]. a.symmetricDifference(b) returns the same elements in the same order, because it lists the receiver's unique elements first and then the argument's.