In this tutorial we will learn how to check if an array contains elements from another Javascript array. I found this short sweet syntax to match all or some elements between two arrays. Or does the element exist in the array?

We use some operators and include operator Very easy to use in this tutorial that we learn The simplest and fastest way to check if an element is present in an array is using the some and include operator See below:


Example


Ex. 1 - OR operator find if any of array2 elements exists in array1. This will return as soon as there is a first match as some method breaks when function returns TRUE

let array1 = ['a', 'b', 'c', 'd', 'e'], array2 = ['a', 'b'];
console.log(array2.some(ele => array1.includes(ele)));


Output:

True

Ex. 2 - AND operator find if all of array2 elements exists in array1. This will return as soon as there is a no first match as some method breaks when function returns TRUE

let array1 = ['a', 'b', 'c', 'd', 'e'], array2 = ['a', 'x'];

console.log(!array2.some(ele => !array1.includes(ele)));

I hope it can help you...