JS: Set.prototype.union

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2024)

setA.union(setB)
  • return the union of 2 sets.
  • setB can be a Map.
const xx = new Set([4, 5, 6])
const yy = new Set([6, 7, 8])
console.log(xx.union(yy))
// Set(5) { 4, 5, 6, 7, 8 }
// union of 2 map's keys

const mapA = new Map([[4, 0], [5, 0], [6, 0]])
const mapB = new Map([[6, 0], [7, 0], [8, 0]])

console.log((new Set(mapA.keys())).union(mapB))
// Set(5) { 4, 5, 6, 7, 8 }

JS set operations