Comparing two lists with filter and includes? Use Set methods
JavaScript Set now has methods for intersection, difference and union, and each returns a new Set. They work in current browsers since June 2024, and the argument must be a Set or set-like object, not an array.

You have a list of permissions each button in your dashboard needs, and the list of permissions the user has. You want to know what they share, what is missing, and whether the user has everything required. The usual answer is a filter with includes for each question. This article shows how the new Set methods answer each question in one call, and where they trip you up if you pass them an array.
What are the new Set methods?
The new Set methods are seven methods that compare two sets like math operations: four return a new Set, and three return true or false.
The MDN page for Set lists them in a table:
Method | Returns | Meaning |
|---|---|---|
| Set | everything in A or B |
| Set | what is in both A and B |
| Set | what is in A and not in B |
| Set | what is in only one of them |
| Boolean | is all of A inside B |
| Boolean | is all of B inside A |
| Boolean | do they share no element at all |
How do you find what two lists share?
intersection() returns a new Set with the elements that are in both sets, in place of filter with includes.
That is how the MDN page for intersection describes it, with this example:
const odds = new Set([1, 3, 5, 7, 9]);
const squares = new Set([1, 4, 9]);
console.log(odds.intersection(squares)); // Set(2) { 1, 9 }For the permissions example, instead of required.filter((p) => userPerms.includes(p)) you write:
const shared = required.intersection(userPerms);How do you find what is missing?
difference() returns a new Set with the elements that are in the first set and not in the second, so it gives you what is missing in one call.
The MDN page for difference uses the same two sets:
console.log(odds.difference(squares)); // Set(3) { 3, 5, 7 }Order matters: required.difference(userPerms) is the permissions the button needs and the user does not have. If you only need a yes or no, required.isSubsetOf(userPerms) returns true when everything required is there.
To merge two lists without duplicates, the MDN page for union gives this example, where 4, present in both, shows up once:
const evens = new Set([2, 4, 6, 8]);
console.log(evens.union(squares)); // Set(6) { 2, 4, 6, 8, 1, 9 }Why doesn't an array work as the argument?
The Set methods require the set you call them on to be a real Set, but the argument only needs to be set-like: it has a size property, a has() method and a keys() method.
The same Set page says arrays are not set-like, because they have no has() method or size property, and their keys() method produces indices instead of elements. So if the permissions come from an API as an array, convert them first:
const userPerms = new Set(user.permissions);A Map, on the other hand, works as an argument, because it has all three, and its keys are what count.
Do they work in every browser?
The new Set methods work across the latest browser versions and devices since June 2024, but they might not work on older devices or browsers.
That is what the Baseline 2024 badge on the intersection, difference and union pages above says, and those pages still mark them Newly available. The Set object itself is much older: its page says Widely available. If your audience is on current browsers, use them directly. If you must support older ones, the safe fallback is filter with has() on a Set, or a polyfill.
Where do you start today?
Search your project for .filter( calls that contain .includes( on another list: each one is an intersection or difference question. Turn both lists into Sets and replace the call with intersection() or difference(), and make sure you are not passing an array as the argument.
If you need to sort the result after comparing, sort a copy with toSorted instead of changing the original, and if you need to split it into groups, use Object.groupBy. If you are still building your JavaScript basics, the 2026 Programmer Roadmap lays out what to learn next.
How many filter and includes pairs in your code are doing the job of intersection right now?
FAQ
Can I pass an array to intersection directly?
No. The argument has to be set-like, with size, has() and keys(). An array has no size and no has(), so convert it first with new Set(array).
Do these methods change the original Set?
No. intersection, difference, union and symmetricDifference return a new Set, and the Set you called them on stays as it was.
What do I use if I must support older browsers?
MDN says they might not work on older devices or browsers. In that case fall back to filter with has() on a Set, or add a polyfill to your project.


