Check If Two Arrays Contain Common Elements In JavaScript

To check if two arrays contain common elements in JavaScript –

  1. Use the some() method on the first array, passing it a function.
  2. The function will check if the element present in arr1 is also present in arr2.
  3. If there is such an element, then it will return true, else false.

Let’s discuss this method in detail below.

how to check if two arrays contain common elements in JavaScript

Check If Two Arrays Contain Common Elements In JavaScript

The some() method can be used to check if an array contains any element that meets a certain criterion.

The some() method takes a callback function as an argument.

This callback function is executed for each element of the array, until it finds one where the callback returns a truthy value.

If such an element is found, then some() returns true, else it returns false.

The callback function takes three arguments – element, index and array.

The syntax of the some() method is as follows:

array.some(function(element, index, array){

  // statements

});

Where,

array – is the array on which some() method is called.

function(element, index, array) – is a callback function that is executed for each element of the array until it finds an element where the callback returns a truthy value.

If such an element is found, then some() returns true, else it returns false.

Here is how to check if two arrays contain common elements in JavaScript –

var arr1 = ["a", "b", "c", "d"];

var arr2 = ["e", "f", "g", "h"];

function checkCommon(arr1, arr2) {

   return arr1.some(function(value) {

   return arr2.indexOf(value) !==-1;

});

}

console.log(checkCommon(arr1, arr2)); // false

In the above example, we have two arrays – arr1 and arr2.

We have defined a function – checkCommon() that takes these two arrays as arguments.

This function returns true if the arrays have common elements, else it returns false.

The indexOf() method is used to find the index of an element in an array.

If the element is not present in the array, then it returns -1.

Thus, when we check if arr2.indexOf(value) !==-1, we are essentially checking if the element is present in arr2 or not.

If it is present, then it returns the index of the element, else -1.

Therefore, if there is any element present in arr1 that is also present in arr2, then some() will return true.

If not, it will return false.

Let’s take an example where there are some common elements between the two arrays –

var arr1 = ["a", "b", "c", "d"];

var arr2 = ["e", "f", "g", "h", "a", "c"];

function checkCommon(arr1, arr2) {

  return arr1.some(function(value) {

  return arr2.indexOf(value) !==-1;

});

}

console.log(checkCommon(arr1, arr2)); // true

In the above example, we have two arrays – arr1 and arr2.

Both these arrays have some common elements – “a” and “c”.

Therefore, when we check if arr1 and arr2 have any common elements using the some() method, it returns true.

You can also use the includes() method to check if two arrays have common elements in JavaScript.

The includes() method checks if an array contains a given element.

It returns true if the element is present in the array, else false.

Let’s take an example to understand this –

var arr1 = ["a", "b", "c", "d"];

var arr2 = ["e", "f", "g", "h"];

function checkCommon(arr1, arr2) {

  return arr1.some(function(value) {

  return arr2.includes(value);

});

}

console.log(checkCommon(arr1, arr2)); // false

In the above example, we have two arrays – arr1 and arr2.

We have defined a function – checkCommon() that takes these two arrays as arguments.

This function returns true if the arrays have common elements, else it returns false.

The includes() method is used to check if an array contains a given element.

If the element is present in the array, it returns true, else false.

Thus, when we check if arr2.includes(value), we are essentially checking if the element is present in arr2 or not.

If it is present, then includes() returns true, else false.

If there is any element present in arr1 that is also present in arr2, then some() will return true.

If not, it will return false.

Let’s take an example where there are some common elements between the two arrays –

var arr1 = ["a", "b", "c", "d"];

var arr2 = ["e", "f", "g", "h", "a", "c"];

function checkCommon(arr1, arr2) {

   return arr1.some(function(value) {

   return arr2.includes(value);

});

}

console.log(checkCommon(arr1, arr2)); // true

In the above example, we have two arrays – arr1 and arr2.

Both these arrays have some common elements – “a” and “c”.

Therefore, when we check if arr1 and arr2 have any common elements using the some() method, it returns true.

Conclusion – Check If Two Arrays Contain Common Elements In JavaScript

In this article, we saw how to check if two arrays contain common elements in JavaScript.

We saw how to use the some() method and the includes() method to achieve this.

I hope this article was helpful.

Happy Coding! 🙂

Leave a Reply