JS Built-ins
← Set65 / 85
Setdifferenceintermediate

Set difference() β€” originals unchanged

πŸ“– Usage Example
const a = new Set([1, 2, 3, 4])
const b = new Set([3, 4, 5])
const diff = a.difference(b)
console.log([...diff]) // β†’ [1, 2]

Use Set.difference() to get elements in the first set that are not in the second.

Set.prototype.difference() (ES2025)

difference() does not modify the original Sets.

Challenge: Verify originals are unchanged after difference().

πŸ’‘ Hints (1)
  • β€’ difference() returns a new Set; neither original is modified