Set.prototype.union
Combines two sets.
Available since: ES2025
Syntax
setA.union(setB)Parameters
| Parameter | Type | Description |
|---|---|---|
| setB | Set | The second set. |
Return value
SetA new Set with the values from both sets.
Examples
All the skills of two people without duplicates:
const anna = new Set(['js', 'css']);
const oleg = new Set(['js', 'php']);
console.log([...anna.union(oleg)].join(', '));Output:
js, css, phpNotes
The original sets are not changed. In older browsers do the same with spread: new Set([...a, ...b]).
See also
- Set.prototype.intersection — Returns the values two sets have in common.
- Set.prototype.difference — Returns the values that are only in the first set.
- Set — A collection of unique values.
In the tutorial
- Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.