Set.prototype.difference
Returns the values that are only in the first set.
Available since: ES2025
Syntax
setA.difference(setB)Parameters
| Parameter | Type | Description |
|---|---|---|
| setB | Set | The set whose values are excluded. |
Return value
SetA new Set with the values of the first set that are not in the second.
Examples
What Anna knows and Oleg does not:
const anna = new Set(['js', 'css', 'sql']);
const oleg = new Set(['js', 'php']);
console.log([...anna.difference(oleg)].join(', '));Output:
css, sqlNotes
Order matters: a.difference(b) and b.difference(a) give different results. Values found in only one of the sets are returned by symmetricDifference().
See also
- Set.prototype.intersection — Returns the values two sets have in common.
- Set.prototype.union — Combines two sets.
- Set.prototype.isSubsetOf — Checks that every value of a set is in another one.
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.