Skip to main content

How to Check if an Array Contains Any Element of Another Array in JavaScript

A common task in data manipulation is to find out if two arrays have at least one element in common. This is useful for checking permissions, comparing tags, or finding overlaps in datasets. You don't need to find all the common elements, you just need to know if an intersection exists.

This guide will teach you the most modern and efficient methods for solving this problem. You will learn how to use the highly readable combination of some() and includes(), and also see a more performant alternative using a Set for larger arrays.

The Core Method: some() with includes()

This is the most modern, concise, and readable way to check for a common element. It combines two powerful array methods.

The logic:

  1. Use the Array.prototype.some() method to iterate over the first array (arr1).
  2. For each element in arr1, use the Array.prototype.includes() method to check if that element exists in the second array (arr2).
  3. The some() method will automatically stop and return true as soon as includes() returns true for any element. If no common elements are found after checking all of them, some() will return false.

Problem: We need to know if arr1 and arr2 share any common food items.

// Problem: Do these arrays have any elements in common?
const arr1 = ['pizza', 'cake', 'cola'];
const arr2 = ['sushi', 'beer', 'cake'];
const arr3 = ['sushi', 'beer'];

Solution:

function haveCommonElement(arr1, arr2) {
return arr1.some(item => arr2.includes(item));
}

// Example Usage:
const arr1 = ['pizza', 'cake', 'cola'];
const arr2 = ['sushi', 'beer', 'cake'];
const arr3 = ['sushi', 'beer'];

console.log(haveCommonElement(arr1, arr2)); // Output: true
console.log(haveCommonElement(arr1, arr3)); // Output: false
note

This approach is highly favored for its clarity. The code reads almost exactly like the problem it's trying to solve: "Does some item in arr1 exist in arr2?"

A More Performant Method for Large Arrays: Using a Set

The some() with includes() method is very readable, but its performance can degrade with large arrays. The includes() method has to search through arr2 for every single element in arr1, which can be slow.

A more performant approach is to first convert one of the arrays into a Set. Checking for an element's existence in a Set (set.has(item)) is significantly faster than searching an array (array.includes(item)).

Solution:

function haveCommonElement(arr1, arr2) {
const set2 = new Set(arr2);
return arr1.some(item => set2.has(item));
}

// Example Usage:
const arr1 = ['pizza', 'cake', 'cola'];
const arr2 = ['sushi', 'beer', 'cake'];

console.log(haveCommonElement(arr1, arr2)); // Output: true
note

For small arrays, the difference is negligible, but for arrays with thousands of items, this Set-based approach is much more efficient.

The Traditional Method: Using a for...of Loop

Before functional methods like some() were common, this problem was solved with a standard for loop. This approach is still perfectly valid and can be more readable for developers who are less familiar with functional programming.

The logic:

  1. Loop through each element of the first array (arr1).
  2. Inside the loop, check if the second array (arr2) includes that element.
  3. If it does, we have our answer. Return true immediately and exit the loop.
  4. If the loop finishes without finding any common elements, return false.

Solution:

function haveCommonElement(arr1, arr2) {
for (const item of arr1) {
if (arr2.includes(item)) {
return true; // Found a common element, exit early
}
}
return false; // No common elements were found
}

// Example Usage:
const arr1 = ['pizza', 'cake', 'cola'];
const arr2 = ['sushi', 'beer', 'cake'];

console.log(haveCommonElement(arr1, arr2)); // Output: true
note

This method is also very efficient because the return true statement ensures the loop stops as soon as a match is found.

Conclusion

Checking if two arrays share at least one element is a common task with several effective solutions in JavaScript.

  • The arr1.some(item => arr2.includes(item)) method is the most concise and readable solution, making it an excellent choice for most situations, especially with small to medium-sized arrays.
  • For large arrays, converting the second array to a Set first (const set2 = new Set(arr2)) and then using some() with set2.has() provides better performance.
  • A traditional for...of loop is also a highly efficient and perfectly acceptable solution that is clear and easy to understand.