Sub topic 2
New Sets In, Originals Untouched
This chapter assumes you can create a Set, call add, has, and delete on it, and know the difference between an object reference and a primitive value. Before looking at each composition method on its own, it helps to learn the one rule they all follow: they never change the sets you call them on or pass to them.
Two Kinds of Set Methods
The Set methods you already know, add, delete, and clear, are mutating. A mutating method changes the set it is called on. The ES2025 methods work differently. They read their inputs and report a result:
| Method group | Methods | Returns | Changes inputs? |
|---|---|---|---|
| Mutating | `add`, `delete`, `clear` | the set, a boolean, or `undefined` | Yes |
| Composition | `union`, `intersection`, `difference`, `symmetricDifference` | a brand-new `Set` | No |
| Predicates | `isSubsetOf`, `isSupersetOf`, `isDisjointFrom` | a boolean | No |
const a = new Set([1, 2, 3]);
const b = new Set([2, 3, 4]);
const shared = a.intersection(b);
console.log([...shared]); // [2, 3]
console.log([...a]); // [1, 2, 3] (unchanged)
console.log([...b]); // [2, 3, 4] (unchanged)
console.log(shared === a); // falseAlways a Fresh Set, Even When Nothing Changes
The result is a new object even when its contents match an input exactly. A union with an empty set still gives you a copy:
const original = new Set(["x", "y"]);
const copy = original.union(new Set());
console.log(copy === original); // false
copy.add("z");
console.log(original.has("z")); // falseThat makes the result safe to change. Adding to or deleting from the result never affects the inputs.
The result is also always a plain Set. If you call union on an instance of a Set subclass, you still get back a plain Set, because these methods do not use Symbol.species:
class TagSet extends Set {}
const tags = new TagSet(["a"]);
const merged = tags.union(new Set(["b"]));
console.log(merged instanceof TagSet); // false
console.log(merged instanceof Set); // trueThe Copy Is Shallow
The result is a new container, but the elements are not cloned. When the elements are objects, the result holds the same references as the input:
const user = { name: "Ana" };
const team = new Set([user]);
const everyone = team.union(new Set());
[...everyone][0].name = "Bea";
console.log(user.name); // "Bea"Common Mistake: Expecting an In-Place Update
Developers used to add often write a composition call and throw away the return value:
let allowed = new Set(["read"]);
allowed.union(new Set(["write"])); // result discarded
console.log(allowed.has("write")); // falseThe fix is to keep the result. Declare the variable with let and reassign it:
let allowed = new Set(["read"]);
allowed = allowed.union(new Set(["write"]));
console.log(allowed.has("write")); // trueIf other code holds a reference to the original set and needs to see the change, reassignment won't reach that code. In that case, mutate the set directly:
const allowed = new Set(["read"]);
for (const p of new Set(["write"])) allowed.add(p);
console.log(allowed.has("write")); // trueWhy This Design Helps
Because the inputs are never touched, you can chain calls without defensive copies:
const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5]);
const c = new Set([4]);
const result = a.union(b).difference(c);
console.log([...result]); // [1, 2, 3, 5]
console.log(a.size, b.size, c.size); // 4 3 1Each step creates an intermediate set and passes it on. For very large sets in hot loops, those extra allocations cost memory and time. For typical application data the cost is negligible.
Exercise
After running const s = new Set([1]); const t = s.union(new Set([2])); t.delete(1);, what does s.has(1) return, and why?
Show answer
It returns true. union returned a new Set t, so deleting from t does not affect s.
Exercise
A function receives perms and runs perms = perms.difference(revoked). The caller's set still contains the revoked permissions. Explain the cause and one fix.
Show answer
difference returns a new Set. Reassigning the local parameter only rebinds that local name, so the caller's set never changes. Fix it by returning the new set and having the caller assign it, or by deleting each revoked item from perms in place with delete.