Favorites

JavaScript Set.prototype.difference() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Set.prototype.difference()
JavaScript Set Instance methodThis method is supported by most modern browsers, though support in some browsers is unknown.
Updated 28th November 2026

The difference() method returns a new Set with the values from the first set that aren't in the second.

Description

The difference() method returns a new set with the values that are in this set but not in the other. Order matters: a.difference(b) is not the same as b.difference(a). Use symmetricDifference() for values in either set but not both.

Typical uses include finding missing required fields, items added or removed between two versions of a list, and permissions a user lacks. To find values in either set but not both, use symmetricDifference().

The argument can be any set-like object, meaning one with a size property and has() and keys() methods, such as another Set or a Map (whose keys are used). Arrays aren't set-like, so wrap them first: new Set(array). The original sets are never changed, and the result keeps the order of the set the method was called on.

Syntax

set.difference(other)

Parameters

  • otherAnother Set, or a set-like object.

Return Value

A new Set with the values in this set that aren't in the other.

Example

const required = new Set(['name', 'email', 'phone']);
const given = new Set(['name', 'phone']);
console.log(required.difference(given));

Browser Support

The following information will show you the current browser support for the Set.prototype.difference() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by most modern browsers, though support in some browsers is unknown.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee