JS Built-ins
← Set61 / 85
Setdifferenceintermediate

Set difference() β€” elements in A not B

πŸ“– 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)

setA.difference(setB) returns a new Set with elements in A but not in B.

Challenge: Verify the difference of two Sets.

πŸ’‘ Hints (1)
  • β€’ A.difference(B) = A - B β€” elements in A that are not in B