Check if Array contains any element of another Array in JS

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
3 min

banner

# Table of Contents

  1. Check if Array contains any element of another Array in JS
  2. Check if Array contains any element of another Array using indexOf()
  3. Check if Array contains any element of another Array using for...of
  4. Check if Array contains any element of another Array using for

# Check if Array contains any element of another Array in JS

To check if an array contains any element of another array:

  1. Use the Array.some() method to iterate over the first array.
  2. Check if each element is contained in the second array.
  3. If there is at least 1 common element, the Array.some() method will return true.
index.js
const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; const contains = arr1.some(element => { return arr2.includes(element); }); console.log(contains); // ๐Ÿ‘‰๏ธ true

check if array contains any element of another array

The code for this article is available on GitHub

The function we passed to the Array.some() method gets called with each element of the array.

If at least one invocation of the callback function returns a truthy value, the some() method returns true, otherwise, false is returned.
index.js
const arr1 = ['pizza', 'cake', 'cola']; console.log(arr1.includes('pizza')); // ๐Ÿ‘‰๏ธ true console.log(arr1.includes('another')); // ๐Ÿ‘‰๏ธ false

The arrays in the example have a common element, so the Array.includes() method returns true, causing the Array.some() method to also return true.

Alternatively, you can use the indexOf() method.

# Check if Array contains any element of another Array using indexOf()

This is a three-step process:

  1. Use the Array.some() method to iterate over the first array.
  2. Use the Array.indexOf() method to check if each element is contained in the second array.
  3. If the Array.indexOf() method returns a value other than -1, the arrays contain common elements.
index.js
const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; const contains = arr1.some(element => { return arr2.indexOf(element) !== -1; }); console.log(contains); // ๐Ÿ‘‰๏ธ true

check if array contains any element of another array using indexof

The code for this article is available on GitHub

If the arrays have a common element, then Array.indexOf will return the element's index, otherwise, it returns -1.

index.js
const arr1 = ['pizza', 'cake', 'cola']; console.log(arr1.indexOf('pizza')); // ๐Ÿ‘‰๏ธ 0 console.log(arr1.indexOf('another')); // ๐Ÿ‘‰๏ธ -1

In the code example, there is a common element between the arrays, so the callback function returns true and the Array.some() method also returns true.

Alternatively, you can use a for...of loop.

# Check if Array contains any element of another Array using for...of

This is a three-step process:

  1. Declare a new variable and initialize it to false.
  2. Use a for...of loop to iterate over the first array.
  3. If any element is contained in the second array, set the variable to true.
App.js
const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; let containsAny = false; for (const element of arr1) { if (arr2.includes(element)) { containsAny = true; break; } } console.log(containsAny); // ๐Ÿ‘‰๏ธ true

check if array contains any element of another array using for of

The code for this article is available on GitHub

Notice that we declared the containsAny variable using let.

This is necessary because variables declared using const cannot be reassigned.

The for...of statement is used to loop over iterable objects like arrays, strings, Map, Set and NodeList objects and generators.

On each iteration, we check if the second array contains the current element.

If the condition is met, we know there is at least 1 common element, so we set the containsAny variable to true and exit the for...of loop.

The break statement terminates the current loop or switch statement.

# Check if Array contains any element of another Array using for

You can also use a basic for loop to check if an array contains any element of another array.

index.js
const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; let containsAny = false; for (let index = 0; index < arr1.length; index++) { if (arr2.includes(arr1[index])) { containsAny = true; break; } } console.log(containsAny); // ๐Ÿ‘‰๏ธ true

check if array contains any element of another array using for loop

The code for this article is available on GitHub

We initialized a boolean variable to false, just like in the previous code sample.

On each iteration, we check if the current element is contained in the second array.

If the condition is met, the arrays contain at least one common element, so we break out of the loop.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev