JavaScript Set union() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Instance method

Definition

set.union(other) returns a new Set that holds every value from both Sets. Because the result is a Set, a value that appears in both is kept only once.

Neither Set is changed. The result starts with the values of set in their order, followed by the new values of other. The argument can be a Set or a set-like object such as a Map, but not an array.

Remember
a.union(b)   → every value from a and b, without duplicates

a = {1, 2, 3}
b = {3, 4, 5}

a.union(b)   → {1, 2, 3, 4, 5}
3 is only listed once

a and b stay unchanged

Syntax

Call it on a Set and pass another Set. Read the answer from the new Set that comes back.

JavaScript
const mine = new Set(["js", "css"]);
const yours = new Set(["css", "python"]);

const all = mine.union(yours);

console.log(Array.from(all).join(", "));   // js, css, python
console.log(mine.size);                    // 2 (unchanged)
CallWhat it does
a.union(b)A new Set with all values of a and b.
a.union(b).union(c)Chain calls to join three Sets.
new Set([...a, ...b])The older way. It works everywhere that Set works.
a.union(new Set(array))Turn an array into a Set first, because an array is not accepted.

A standard JavaScript method (ES2025) on every Set, from Set.prototype. It works in current browsers and in Node.js 22 or newer. Siblings: add(), clear(), delete(), difference(), entries(), forEach(), has(), intersection(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), keys(), symmetricDifference(), values(), [Symbol.iterator]().

Library declarations
JavaScript
// ECMAScript 2025
Set.prototype.union(other)

// other  : a Set, or a set-like object (needs size, has() and keys())
// returns: a new Set
// throws : TypeError if other is not set-like (for example an array)

Parameters

ParameterDescription
otherRequired. A Set, or any set-like object: an object with a numeric size and the methods has() and keys(). A Map is set-like. An array is not, so convert it with new Set(array).

Return Value

Return ValueDescription
SetA new Set with every value from both Sets, without duplicates. The values of the first Set come first. Both original Sets stay as they were.
TypeError (thrown)other is not set-like, for example an array or a number.

JavaScript Examples

Example 1 — Join Two Sets

Two people list the skills they know. Make one list with every skill, with no repeats.

JavaScript
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);
Try It Yourself

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

  1. Two small Sets. Each has two skills, and css is in both of them.
  2. Join them. mine.union(yours) keeps the values of mine first and then adds what is new from yours. css is only listed once, so the size is 3.
  3. Originals are safe. Both source Sets still have 2 values each, because the method never changes them.

Example 2 — Order and Chaining

Change which Set comes first and see the order change. Then join three Sets by chaining.

JavaScript
const a = new Set([1, 2, 3]);
const b = new Set([3, 4, 5]);
const c = new Set([5, 6]);

console.log("a, b: " + Array.from(a.union(b)).join(", "));
console.log("b, a: " + Array.from(b.union(a)).join(", "));

const all = a.union(b).union(c);
console.log("All three: " + Array.from(all).join(", "));
console.log("Same size both ways: " + (a.union(b).size === b.union(a).size));
Try It Yourself

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

  1. Order follows the first Set. a.union(b) starts with 1, 2, 3. b.union(a) starts with 3, 4, 5, so the order is different but the values are the same.
  2. Chain the calls. a.union(b) is a new Set, so you can call union(c) on it. The shared 5 is only kept once.
  3. Same size. Whatever the direction, the result has the same number of values.

Example 3 — Use a Map or an Array

A Map can be the argument, and its keys are added. An array must be wrapped in a Set first.

JavaScript
const tags = new Set(["js", "css"]);
const extra = new Map([["html", 1], ["css", 2]]);

console.log("With a Map: " + Array.from(tags.union(extra)).join(", "));

try {
  tags.union(["sql"]);
} catch (err) {
  console.log("With an array: " + err.name);
}

const fixed = tags.union(new Set(["sql"]));
console.log("Array fixed: " + Array.from(fixed).join(", "));
Try It Yourself

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

  1. A Map is set-like. Its keys html and css are joined with tags, and the repeated css is kept once.
  2. An array is not. An array has no has() method, so union(["sql"]) throws a TypeError.
  3. The fix. Wrap the array in new Set(...), and the join works.

Browser & Runtime Support

Set.prototype.union() is one of the new Set methods added in ES2025. It works in current browsers and in Node.js 22 or newer, but not in older versions.

✓ Newer · ES2025

set.union()

Supported in Chrome 122+, Edge 122+, Firefox 127+, Safari 17+, Opera 108+ and Node.js 22+. Internet Explorer never got it.

ES2025 Standard since
Google Chrome 122+ · Desktop & Mobile
Full support
Microsoft Edge 122+ · Chromium based
Full support
Mozilla Firefox 127+ · Desktop & Mobile
Full support
Apple Safari 17+ · macOS & iOS
Full support
Opera 108+ · Chromium based
Full support
Samsung Internet 26+ · Android
Supported
Android WebView 122+ · Chromium based
Supported
Node.js 22+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
set.union() Recent browsers

Bottom line: Check support with typeof Set.prototype.union === "function". For older browsers, the same result can be made with the spread syntax and filter(), or with a loop.

Frequently Asked Questions

It returns a new Set with every value from both Sets, with no duplicates. It is the Set version of "A or B". Neither original Set is changed.
No. It returns a new Set. If you want the result in a variable, assign it: const all = a.union(b), or a = a.union(b) if a is not a const.
They give the same values in the same order. union() is shorter and does not need an array in between, but it needs ES2025 support. The spread version works in every browser that supports Set.
No. The argument must be a Set or a set-like object such as a Map. An array throws a TypeError. Use new Set(array) to convert it first.
First the values of the Set you call it on, in their order, then the new values of the argument. So a.union(b) and b.union(a) have the same values but may list them in a different order.

Next: values()

Learn how to get an iterator of every value in a Set and turn it into an array.

values() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful