coderef

Set methodsBaseline · Newly available

The difference(), intersection(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), symmetricDifference(), and union() methods of the JavaScript Set object performs operations between two sets.

Works in the latest version of every major browser, but older devices may not support it yet. Baseline since Jun 11, 2024.

Browser support

DesktopChrome
1–121Not supported
122+Supportedsince Feb 20, 2024
Current: 155
Edge
12–121Not supported
122+Supportedsince Feb 23, 2024
Current: 154
Firefox
1–126Not supported
127+Supportedsince Jun 11, 2024
Current: 157
Safari
1–16.6Not supported
17+Supportedsince Sep 18, 2023
Current: 27
MobileChrome Android
18–121Not supported
122+Supportedsince Feb 20, 2024
Current: 155
Firefox Android
4–126Not supported
127+Supportedsince Jun 11, 2024
Current: 157
Safari iOS
1–16.6Not supported
17+Supportedsince Sep 18, 2023
Current: 27

How to use

Every current browser supports this, but it only became Baseline on Jun 11, 2024. Use it as a progressive enhancement, or keep a fallback if you support older devices.

Example

JS
const a = new Set([1, 2, 3]);
const b = new Set([2, 3, 4]);

a.union(b);               // Set {1, 2, 3, 4}
a.intersection(b);        // Set {2, 3}
a.difference(b);          // Set {1}
a.isSubsetOf(new Set([1, 2, 3, 4])); // true

Feature detection

JS · check before using
if ("difference" in Set || ("prototype" in Set && "difference" in Set.prototype)) {
  // Set.difference is available
}

Sub-features

Support for each part of the feature (7). The overall table above shows when the whole feature works.

Sub-featureChromeEdgeFirefoxSafariChrome AndroidFirefox AndroidSafari iOS
Set.difference1221221271712212717
Set.intersection1221221271712212717
Set.isDisjointFrom1221221271712212717
Set.isSubsetOf1221221271712212717
Set.isSupersetOf1221221271712212717
Set.symmetricDifference1221221271712212717
Set.union1221221271712212717