• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Find Common Elements in Two Arrays Using JavaScript

Find Common Elements in Two Arrays Using JavaScript

August 29, 2022 Leave a Comment

To find common elements in two arrays in JavaScript, we simply need to make use of the JavaScript Array filter() and includes() methods. Here is the code to get this done.

var commonElementsArray = firstArray.filter(element => secondArray.includes(element));

Let’s add to this code and show an example to show how easy it is to find common elements in two arrays using JavaScript.


Let’s show the above code above being used on a couple of simple arrays of numbers.

var firstArray = [1,2,3,4,5];
var secondArray = [1,3,5];

var commonElementsArray = firstArray.filter(element => secondArray.includes(element));

console.log(commonElementsArray);

#Output
[1, 3, 5]

You can see the code above will create a new array for us via the filter() method. We can wrap this code in a function to make it really easy to reuse it to find common elements in two arrays.

We will simply call our function commonArrayElements(), which will take two parameters, the two arrays we want to look at. It will then return a single array with all of the common elements of both arrays.

Here is our function:

function commonArrayElements(array1,array2){
  return array1.filter(element => array2.includes(element));
};

Now let’s show some examples of this function in action.

function commonArrayElements(array1,array2){
  return array1.filter(element => array2.includes(element));
};

var array1 = [0,1,2,3,4,5,6,7,8,9];
var array2 = ['a','b','c','d','e','f'];
var array3 = [1,'a',-1,'b',-2,'e',false,"4","hello","Hello"];
var array4 = [-2,-4,-6,-8];
var array5 = ["hello",4,false,"hello"];

console.log(commonArrayElements(array1,array2));
console.log(commonArrayElements(array1,array3));
console.log(commonArrayElements(array2,array3));
console.log(commonArrayElements(array3,array4));
console.log(commonArrayElements(array3,array5));

#Output
[]
[1]
['a', 'b', 'e']
[-2]
[false, 'hello']

Check out how to use the filter() method in a similar way to find the number of even numbers in an array.

Hopefully this article has been useful for you to learn how to use JavaScript to find common elements in two arrays in JavaScript.

Other Articles You'll Also Like:

  • 1.  Convert a Set to Array in JavaScript
  • 2.  How to Call a JavaScript Function From HTML
  • 3.  JavaScript Square Root with Math.sqrt() Method
  • 4.  Using JavaScript to Run a Function Every 5 seconds
  • 5.  Remove String From Array in JavaScript
  • 6.  JavaScript toFixed – How to Convert a Number to a String
  • 7.  Remove Undefined Values From an Array in JavaScript
  • 8.  Remove Braces from String Using JavaScript
  • 9.  Using JavaScript to Rotate an Image
  • 10.  Using Javascript to Remove the Id from a Div

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy