Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Set.prototype.union

Combines two sets.

Available since: ES2025

Syntax

setA.union(setB)

Parameters

ParameterTypeDescription
setBSetThe 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, php

Notes

The original sets are not changed. In older browsers do the same with spread: new Set([...a, ...b]).

See also

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.

Thanks for reading!

Donate to the site — we work hard for you.