Use a combination of Array.prototype.filter and Array.prototype.includes:

const filteredArray = array1.filter(value => array2.includes(value));

For older browsers, with Array.prototype.indexOf and without an arrow function:

var filteredArray = array1.filter(function(n) {
    return array2.indexOf(n) !== -1;
});

NB! Both .includes and .indexOf internally compares elements in the array by using ===, so if the array contains objects it will only compare object references (not their content). If you want to specify your own comparison logic, use Array.prototype.some instead.

Answer from Anon. on Stack Overflow
๐ŸŒ
DEV Community
dev.to โ€บ rajeshkumaryadavdotcom โ€บ intersection-and-union-of-array-in-javascript-2mg8
Intersection and Union of Array in JavaScript - DEV Community
September 20, 2021 - #javascript #interview ยท Union ... console.log(arrUnion); The intersection of two arrays is a list of distinct numbers which are present in both the arrays....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-program-to-find-union-and-intersection-of-two-unsorted-arrays
JavaScript Program to Find Union and Intersection of Two Unsorted Arrays - GeeksforGeeks
July 23, 2025 - Space Complexity: O(n + m) for the union and O(min(n, m)) for the intersection. Creating functions and passing both arrays and their respective lengths.
Discussions

data structures - Simplest code for array intersection in javascript - Stack Overflow
If the array is not sorted, need to loop around 1,000,000 times when you intersect 1000 length array x 1000 length array 2009-12-11T14:49:30.737Z+00:00 ... I think you missed my point, which is that arbitrary objects in JavaScript have no natural sort order, meaning that sorting an array of ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Getting a union of two arrays in JavaScript - Stack Overflow
Say I have an array of [34, 35, 45, 48, 49] and another array of [48, 55]. How can I get a resulting array of [34, 35, 45, 48, 49, 55]? More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - How to perform union or intersection on an array of arrays with Underscore.js - Stack Overflow
Underscore.js has convenient union and intersection methods but they work on passing each array individually as arguments. More on stackoverflow.com
๐ŸŒ stackoverflow.com
March 10, 2016
How to calculate the intersection of multiple arrays in JavaScript
You skip over the interesting part of object comparisons (comparing the objects) entirely by going for a "key" property (without explaining why), and go directly to code (with, I think, at least one bug) instead of explaining concepts. I think this would be a better and more useful read if you included more background, theory, and practical comparisons (beyond just the order of elements). More on reddit.com
๐ŸŒ r/javascript
12
23
March 15, 2023
๐ŸŒ
Medium
medium.com โ€บ @alvaro.saburido โ€บ set-theory-for-arrays-in-es6-eb2f20a61848
Array intersection, difference, and union in ES6 | by Alvaro Saburido | Medium
August 2, 2020 - But, there is a problem is that if we use spread operator, we will get elements duplicated, so itโ€™s no theoretically a union. For doing this we have can use a Set to help us out: ... Why? In the article I mentioned earlier, Sets in javascript contains only distinct elements.
๐ŸŒ
GitHub
gist.github.com โ€บ jfollmann โ€บ b065d1ebb9043cacfbe9249a572bbd7e
Array intersection, difference, and union in ES6 ยท GitHub
Array intersection, difference, and union in ES6 ยท Raw ยท intersection-difference-union.js ยท This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Top answer
1 of 16
1960

Use a combination of Array.prototype.filter and Array.prototype.includes:

const filteredArray = array1.filter(value => array2.includes(value));

For older browsers, with Array.prototype.indexOf and without an arrow function:

var filteredArray = array1.filter(function(n) {
    return array2.indexOf(n) !== -1;
});

NB! Both .includes and .indexOf internally compares elements in the array by using ===, so if the array contains objects it will only compare object references (not their content). If you want to specify your own comparison logic, use Array.prototype.some instead.

2 of 16
163

Destructive seems simplest, especially if we can assume the input is sorted:

/* destructively finds the intersection of 
 * two arrays in a simple fashion.  
 *
 * PARAMS
 *  a - first array, must already be sorted
 *  b - second array, must already be sorted
 *
 * NOTES
 *  State of input arrays is undefined when
 *  the function returns.  They should be 
 *  (prolly) be dumped.
 *
 *  Should have O(n) operations, where n is 
 *    n = MIN(a.length, b.length)
 */
function intersection_destructive(a, b)
{
  var result = [];
  while( a.length > 0 && b.length > 0 )
  {  
     if      (a[0] < b[0] ){ a.shift(); }
     else if (a[0] > b[0] ){ b.shift(); }
     else /* they're equal */
     {
       result.push(a.shift());
       b.shift();
     }
  }

  return result;
}

Non-destructive has to be a hair more complicated, since weโ€™ve got to track indices:

/* finds the intersection of 
 * two arrays in a simple fashion.  
 *
 * PARAMS
 *  a - first array, must already be sorted
 *  b - second array, must already be sorted
 *
 * NOTES
 *
 *  Should have O(n) operations, where n is 
 *    n = MIN(a.length(), b.length())
 */
function intersect_safe(a, b)
{
  var ai=0, bi=0;
  var result = [];

  while( ai < a.length && bi < b.length )
  {
     if      (a[ai] < b[bi] ){ ai++; }
     else if (a[ai] > b[bi] ){ bi++; }
     else /* they're equal */
     {
       result.push(a[ai]);
       ai++;
       bi++;
     }
  }

  return result;
}
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ javascript โ€บ array โ€บ set operations
Set operations in JavaScript - 30 seconds of code
January 1, 2024 - In order to calculate it, you can ... 3]), new Set([4, 3, 2])); // Set(4) { 1, 2, 3, 4 } The intersection of two sets is a set containing all elements that exist in both sets....
Find elsewhere
๐ŸŒ
GitHub
gist.github.com โ€บ ackhare โ€บ 594277708ddc8b74c3c5448a034de8b9
Array intersection, difference and union in ES6 (javascript) ยท GitHub
Array intersection, difference and union in ES6 (javascript) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-get-union-of-two-arrays
Get the Union of Two Arrays or Sets in JavaScript | bobbyhadz
Use the spread syntax (...) to unpack the values of the Sets into an array. Pass the result to the Set() constructor. The new Set will contain the union of the other two.
๐ŸŒ
GitHub
github.com โ€บ seapp88 โ€บ es6_array
GitHub - seapp88/es6_array: Array intersection, difference and union in ES6 ยท GitHub
let intersection = arrA.filter(x => arrB.includes(x)); The difference will output the elements from array A that are not in the array B.
Author: seapp88
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-compute-union-of-javascript-arrays
JavaScript - Union of Arrays - GeeksforGeeks
In JavaScript, finding the union of multiple arrays means combining their elements into a single array with only unique values.
Published: July 12, 2025
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Set โ€บ union
Set.prototype.union() - JavaScript - MDN Web Docs
September 1, 2026 - The union() method of Set instances takes a set and returns a new set containing elements which are in either or both of this set and the given set.
๐ŸŒ
Medium
medium.com โ€บ just-javascript-tutorials โ€บ javascript-union-intersection-and-difference-with-es6-set-13b953b21f62
JavaScript Union, Intersection, and Difference With ES6 Set ๐Ÿš€
October 2, 2023 - The resulting set contains all elements from both original sets. Intersection: The intersection operation finds the common elements between two sets, creating a new set containing only those elements present in both original sets.
๐ŸŒ
Mahdhi Rezvi's Blog
mahdhir.hashnode.dev โ€บ array-intersection-difference-and-union-with-javascript-es7
Array Intersection, Difference, and Union with JavaScript | by Mahdhi
January 12, 2020 - We can simply use the JavaScript Spread operator to create a union. ... If you note carefully, you'll see that some of the intersecting elements have been repeated in the above output.
๐ŸŒ
RealCoding Tech Blog
realcoding.blog โ€บ 2026 โ€บ 03 โ€บ 22 โ€บ javascript-set-methods-intersection-union-en
JavaScript Set New Methods - intersection, union, difference Practical Guide | RealCoding Tech Blog
March 22, 2026 - const frontend = new Set(['React', 'Vue', 'Svelte', 'Angular']); const liked = new Set(['React', 'Svelte', 'Rust', 'Go']); // Intersection - elements in both frontend.intersection(liked); // Set {'React', 'Svelte'} // Union - all elements combined frontend.union(liked); // Set {'React', 'Vue', 'Svelte', 'Angular', 'Rust', 'Go'} // Difference - only in frontend frontend.difference(liked); // Set {'Vue', 'Angular'} // Symmetric difference - in one but not both frontend.symmetricDifference(liked); // Set {'Vue', 'Angular', 'Rust', 'Go'}
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-create-an-array-using-intersection-of-two-arrays-in-javascript
How to Create an Array using Intersection of two Arrays in JavaScript ? | GeeksforGeeks
If the array is already sorted, ... In JavaScript, finding the union of multiple arrays means combining their elements into a single array with only unique values....
Published: May 21, 2024
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ how to calculate the intersection of multiple arrays in javascript
r/javascript on Reddit: How to calculate the intersection of multiple arrays in JavaScript
March 15, 2023 - Intersecting Set is linear and only grows with your smallest set. This solution appears to be linear but you have to iterate over every value in all compared arrays.
๐ŸŒ
Diego Betto
diegobetto.com โ€บ home โ€บ blog โ€บ javascript's new set methods: union, intersection, difference, and the rest
JavaScript's new Set methods: union, intersection, difference, and the rest โ€” Diego Betto
1 month ago - Native Set operations replace a class of hand-rolled utility function that nearly every JavaScript codebase eventually accumulates. union, intersection, difference, symmetricDifference, and the three subset/superset/disjoint predicates cover the vast majority of real-world set comparisons โ€” reach for them directly instead of writing filter+includes helpers from scratch.