Example 1 — Join Two Sets
Two people list the skills they know. Make one list with every skill, with no repeats.
const mine = new Set(["js", "css"]);
const yours = new Set(["css", "python"]);
const all = mine.union(yours);
console.log("All: " + Array.from(all).join(", "));
console.log("All size: " + all.size);
console.log("Mine size: " + mine.size);
console.log("Yours size: " + yours.size); The Try It page first checks that Set.prototype.union() exists, because it is new in ES2025. In an older browser it prints a short explanation and the output above. In current browsers and in Node.js 22 or newer, it runs the code for real.
How It Works
- Two small Sets. Each has two skills, and
cssis in both of them. - Join them.
mine.union(yours)keeps the values ofminefirst and then adds what is new fromyours.cssis only listed once, so the size is 3. - Originals are safe. Both source Sets still have 2 values each, because the method never changes them.

