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
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;
}
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › intersection
Set.prototype.intersection() - JavaScript | MDN
May 22, 2026 - The intersection() method of Set instances takes a set and returns a new set containing elements in both this set and the given set.
🌐
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 - Iterate through arr1 and use binary search to check if each element exists in the larger array, arr2. If found in both arrays, add it to the ans array. Example: This example describes the union & intersection of 2 unsorted Arrays by implementing Sorting and searching.
🌐
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 - The intersection will give us the elements that both arrays share in common, in this case, the result must be [1,5].
🌐
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.
🌐
GitHub
github.com › lovasoa › fast_array_intersect
GitHub - lovasoa/fast_array_intersect: The fastest javascript array intersection function · GitHub
By default, the intersection is computed for values that are equal under SameValueZero. You can use the optional hash function argument when you are intersecting arrays of javascript objects.
Author: lovasoa
Find elsewhere
🌐
W3Schools
w3schools.com › jsref › jsref_set_intersection.asp
JavaScript Set intersection() Method
The intersection() method returns a new set containing the elements which are in this set and in the argument set:
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-find-intersection-of-unsorted-arrays
JavaScript Program to find Intersection of Unsorted Arrays - GeeksforGeeks
July 23, 2025 - JavaScript arrays are versatile structures used to store collections of elements. Oftentimes, these arrays may contain duplicates, and finding the common elements among them, known as the "intersection," becomes necessary.
🌐
Medium
medium.com › @saintyommex › how-to-find-common-elements-the-intersection-between-two-arrays-in-javascript-8bac774987d2
How to find common elements (the intersection) between two arrays in JavaScript | by VictorMaria Ajayi | Medium
November 11, 2023 - Run a loop on the second array and in each iteration check if the current element is in the object you created, if so put it to the array where you would keep the common elements i.e intersection.
🌐
4Geeks
4geeks.com › how-to › javascript-array-intersection
How to find the interception of Arrays in Javascript?
July 16, 2025 - Learn how to find the intersection of two or more arrays in Javascript. Discover different methods for finding common elements and optimizing your code.
🌐
Godot Forum
forum.godotengine.org › archive
How to check for array intersection? - Archive - Godot Forum
June 7, 2018 - ℹ Attention Topic was automatically imported from the old Question2Answer platform. 👤 Asked By Diet Estus I have two arrays, each containing a few thousand Vector2s. I want to find out if they intersect, but don’t care about the intersection itself. Currently, I do it this way. func intersects(array1, array2): var intersect = false for item in array1: if item in array2: intersect = true break ret...
🌐
Leaflet
leafletjs.com › reference.html
Documentation - Leaflet - a JavaScript library for interactive maps
You can also pass a multi-dimensional array to represent a MultiPolyline shape:
🌐
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.
🌐
Programiz
programiz.com › javascript › examples › intersection-two-arrays
JavaScript Program To Perform Intersection Between Two Arrays
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to perform intersection between two arrays using Set // intersection contains the elements of array1 that are also in array2 function performIntersection(arr1, arr2) { // converting into Set const setA = new Set(arr1); const setB = new Set(arr2); let intersectionResult = []; for (let i of setB) { if (setA.has(i)) { intersectionResult.push(i); } } return intersectionResult; } const array1 = [1, 2, 3, 5, 9]; const array2 = [1, 3, 5, 8]; const result = performIntersection(array1, array2); console.log(result);
🌐
Underscore.js
underscorejs.org › docs › modules › intersection.html
intersection.js
Produce an array that contains every item shared between all the passed-in arrays. export default function intersection(array) { var result = []; var argsLength = arguments.length; for (var i = 0, length = getLength(array); i < length; i++) { var item = array[i]; if (contains(result, item)) ...
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › objects.html
TypeScript: Documentation - Object Types
We just looked at two ways to combine types which are similar, but are actually subtly different. With interfaces, we could use an extends clause to extend from other types, and we were able to do something similar with intersections and name the result with a type alias.
🌐
DEV Community
dev.to › a_b_102931 › finding-the-intersection-of-two-arrays-233l
Finding the Intersection of Two Arrays - DEV Community
June 1, 2020 - function arrayIntersect(numbersOne, numbersTwo) { const interSection = new Set() for(let number of numbersTwo) { if (numbersOne.includes(number)) { interSection.add(number) } } return [...interSection] } For further actions, you may consider blocking this person and/or reporting abuse ... #javascript #operators #beginners #technicalinterviews Back to Basics: Functions, Hoisting, and Scope
🌐
TutorialsPoint
tutorialspoint.com › article › simplest-code-for-array-intersection-in-javascript
Simplest code for array intersection in JavaScript?
March 15, 2026 - var firstNamesArray = ["John", "David", "Bob", "Sam", "Carol"]; var secondNamesArray = ["Mike", "Carol", "Adam", "David"]; var intersectionOfArray = firstNamesArray.filter(v => secondNamesArray.includes(v)); console.log("Intersection of two arrays:"); console.log(intersectionOfArray);
🌐
Medium
medium.com › @nileslabs › mastering-javascript-array-intersection-boosting-efficiency-with-real-world-examples-720ecbad6069
Mastering JavaScript Array Intersection: Boosting Efficiency with Real-World Examples | by Nilesh Kumar | Medium
February 8, 2024 - Introduction: Navigating the realm of JavaScript array operations is crucial for every coder. One common task is finding the intersection of two arrays efficiently.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › intersection-of-two-arrays
Intersection of two Arrays - GeeksforGeeks
Given two arrays a[] and b[], find their intersection — the unique elements that appear in both.
Published: July 27, 2025