JavaScript Set intersection() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Instance method

Definition

set.intersection(other) returns a new Set with the values that are in both Sets. It answers the question: "What do these two collections have in common?"

Neither Set is changed. The argument can be another Set or a set-like object such as a Map, but not an array. The result has the same values whichever Set you start from, but the order of the values can differ.

Remember
a.intersection(b)   → values that are in a AND in b

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

a.intersection(b)   → {3, 4}
b.intersection(a)   → {3, 4}   (same values)

a and b stay unchanged

Syntax

Call it on a Set and pass another Set. Read the answer from the new Set that comes back.

JavaScript
const mine = new Set(["js", "css", "html"]);
const yours = new Set(["css", "python", "js"]);

const common = mine.intersection(yours);

console.log(Array.from(common).join(", "));   // js, css
console.log(mine.size);                       // 3 (unchanged)
CallWhat it does
a.intersection(b)A new Set with the values that are in both a and b.
a.intersection(b).sizeHow many values they share. 0 means nothing in common.
a.intersection(b).intersection(c)Chain calls to find values that are in all three Sets.
a.intersection(new Set(array))Turn an array into a Set first, because an array is not accepted.

A standard JavaScript method (ES2025) on every Set, from Set.prototype. It works in current browsers and in Node.js 22 or newer. Siblings: add(), clear(), delete(), difference(), entries(), forEach(), has(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), keys(), symmetricDifference(), union(), values(), [Symbol.iterator]().

Library declarations
JavaScript
// ECMAScript 2025
Set.prototype.intersection(other)

// other  : a Set, or a set-like object (needs size, has() and keys())
// returns: a new Set
// throws : TypeError if other is not set-like (for example an array)

Parameters

ParameterDescription
otherRequired. A Set, or any set-like object: an object with a numeric size and the methods has() and keys(). A Map is set-like. An array is not, so convert it with new Set(array).

Return Value

Return ValueDescription
SetA new Set with the values that are in both Sets. It is empty when nothing is shared. Both original Sets stay as they were.
TypeError (thrown)other is not set-like, for example an array or a number.

JavaScript Examples

Example 1 — Find the Values Two Sets Share

Two people list the skills they know. Find the skills they have in common.

JavaScript
const mine = new Set(["js", "css", "html"]);
const yours = new Set(["css", "python", "js"]);

const common = mine.intersection(yours);

console.log("Common: " + Array.from(common).join(", "));
console.log("Common size: " + common.size);
console.log("Mine size: " + mine.size);
console.log("Yours size: " + yours.size);
Try It Yourself

The Try It page first checks that Set.prototype.intersection() exists, because it is new in ES2025. In an older browser it prints a short explanation and the output above. In current browsers and in Node.js 22 or newer, it runs the code for real.

How It Works

  1. Two Sets of skills. mine and yours each have three values, and two of them (js and css) appear in both.
  2. Keep what is shared. mine.intersection(yours) returns a new Set with only js and css.
  3. Originals are safe. Both source Sets still have 3 values, because the method never changes them.

Example 2 — Order and Special Cases

A larger Set can give its common values in the smaller Set's order. Then see what happens with no overlap and with the same Set.

JavaScript
const big = new Set([1, 2, 3, 4]);
const small = new Set([4, 3]);

console.log("big, small: " + Array.from(big.intersection(small)).join(", "));
console.log("small, big: " + Array.from(small.intersection(big)).join(", "));

const odd = new Set([1, 3, 5]);
const even = new Set([2, 4, 6]);
console.log("No overlap: " + odd.intersection(even).size);
console.log("With itself: " + odd.intersection(odd).size);
Try It Yourself

The Try It page first checks that Set.prototype.intersection() exists, because it is new in ES2025. In an older browser it prints a short explanation and the output above. In current browsers and in Node.js 22 or newer, it runs the code for real.

How It Works

  1. Same values, maybe a new order. Both calls find 3 and 4. When the starting Set is larger, the result can follow the smaller Set, so you see 4, 3 in both lines.
  2. No overlap. Odd and even numbers share nothing, so the result is an empty Set with size 0.
  3. With itself. A Set shares every value with itself, so the result has all 3 values.

Example 3 — Chain Calls and Use a Map

Find values that are in three Sets by chaining. A Map can be the argument too, and its keys are compared.

JavaScript
const a = new Set([1, 2, 3, 4, 5]);
const b = new Set([2, 3, 4, 8]);
const c = new Set([3, 4, 9]);

const all = a.intersection(b).intersection(c);
console.log("In all three: " + Array.from(all).join(", "));

const prices = new Map([[3, "low"], [5, "high"], [7, "mid"]]);
const found = a.intersection(prices);
console.log("In the Map keys: " + Array.from(found).join(", "));
Try It Yourself

The Try It page first checks that Set.prototype.intersection() exists, because it is new in ES2025. In an older browser it prints a short explanation and the output above. In current browsers and in Node.js 22 or newer, it runs the code for real.

How It Works

  1. First step. a.intersection(b) keeps 2, 3, 4, a new Set.
  2. Second step. Calling intersection(c) on that new Set keeps only the values that are also in c: 3, 4.
  3. A Map as the argument. A Map is set-like, so its keys 3, 5, 7 are compared with a, and 3, 5 are in both.

Browser & Runtime Support

Set.prototype.intersection() is one of the new Set methods added in ES2025. It works in current browsers and in Node.js 22 or newer, but not in older versions.

✓ Newer · ES2025

set.intersection()

Supported in Chrome 122+, Edge 122+, Firefox 127+, Safari 17+, Opera 108+ and Node.js 22+. Internet Explorer never got it.

ES2025 Standard since
Google Chrome 122+ · Desktop & Mobile
Full support
Microsoft Edge 122+ · Chromium based
Full support
Mozilla Firefox 127+ · Desktop & Mobile
Full support
Apple Safari 17+ · macOS & iOS
Full support
Opera 108+ · Chromium based
Full support
Samsung Internet 26+ · Android
Supported
Android WebView 122+ · Chromium based
Supported
Node.js 22+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
set.intersection() Recent browsers

Bottom line: Check support with typeof Set.prototype.intersection === "function". For older browsers, the same result can be made with the spread syntax and filter(), or with a loop.

Frequently Asked Questions

It returns a new Set with the values that are in both Sets, so it finds what two collections have in common. Neither original Set is changed.
The same values are always returned, but their order can follow the smaller Set when you start from the larger one. Do not depend on the order. Sort the values after converting to an array if you need a fixed order.
No. The argument must be a Set or a set-like object, such as a Map, because the method needs size, has() and keys(). An array throws a TypeError. Use new Set(array) to convert it first.
Chain the calls: a.intersection(b).intersection(c). Each call returns a new Set, so you can call intersection() on the result again.
Use filter() on the first Set: new Set([...a].filter((x) => b.has(x))). It gives the same values and works everywhere that Set works.

Next: isDisjointFrom()

Learn how to check that two Sets have nothing in common, with a simple true or false answer.

isDisjointFrom() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful