The intersection means get elements of arrays that are common. Simply use filter() and includes() method to do JavaScript Array intersection.
Example of JavaScript Array intersection
Use a combination of Array.prototype.filter
and Array.prototype.includes
:
1 |
const filteredArray = array1.filter(value => array2.includes(value)); |
Let’s see simple HTML example of it:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!DOCTYPE html> <html> <head> <script type="text/javascript"> //test array1 = [1,2,3] array2 = [2,3,4,5,6] var filteredArray = array1.filter(function(n) { return array2.indexOf(n) !== -1; }); alert(filteredArray) </script> </head> <body> </body> </html> |
Output:

Source: Stackoverflow.com
Do comment if you have another examples or doubts or suggestion on this topic.
Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version