Set.prototype.intersection
Returns the values two sets have in common.
Available since: ES2025
Syntax
setA.intersection(setB)Parameters
| Parameter | Type | Description |
|---|---|---|
| setB | Set | The second set. |
Return value
SetA new Set with the values found in both sets.
Examples
Common skills:
const anna = new Set(['js', 'css', 'sql']);
const oleg = new Set(['js', 'php', 'sql']);
console.log([...anna.intersection(oleg)].join(', '));Output:
js, sqlNotes
For arrays the same is done with a.filter((x) => b.includes(x)).
See also
- Set.prototype.union — Combines two sets.
- Set.prototype.difference — Returns the values that are only in the first set.
- Array.prototype.filter — Creates a new array from the elements that pass a test.
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.