Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Set.prototype.difference

Returns the values that are only in the first set.

Available since: ES2025

Syntax

setA.difference(setB)

Parameters

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

Notes

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

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.