Check if Multiple Values exist in Array in JavaScript

avatar

Borislav Hadzhiev

Thu Oct 21 20212 min read

banner

Photo by Joss Woodhead

Check if Multiple Values exist in Array #

To check if multiple values exist in an array:

  1. Use the every() method to iterate over the array of values.
  2. On each iteration check if the value is contained in the other array.
  3. If all values exist in the array, the every method will return true.
index.js
const arr = ['one', 'two', 'three', 'four']; const values = ['one', 'two']; const multipleExist = values.every(value => { return arr.includes(value); }); console.log(multipleExist); // ๐Ÿ‘‰๏ธ true

The function we passed to the Array.every method gets called with each element in the values until it returns a falsy value or iterates over the entire array.

On each iteration, we check if the value is contained in the other array, using the Array.includes method.

If even a single value is not contained in the array, the includes method returns false and the every method short-circuits also returning false.

Here is a reusable function that implements this approach.

index.js
function multipleExist(arr, values) { return values.every(value => { return arr.includes(value); }); } // ๐Ÿ‘‡๏ธ true console.log(multipleExist([1, 2, 3], [1, 3])); // ๐Ÿ‘‡๏ธ false console.log(multipleExist([1, 2, 3], [1, 10])); // ๐Ÿ‘‰๏ธ false
The includes method is not supported in Internet Explorer, if you have to support the browser, use the indexOf method instead.

To check if multiple values exist in an array:

  1. Use the every() method to iterate over the array of values.
  2. On each iteration, use the indexOf method to check if the value is contained in the other array.
  3. If all values exist in the array, the every method will return true.
index.js
// Supported in IE 9-11 const arr = ['one', 'two', 'three', 'four']; const values = ['one', 'two']; const multipleExist = values.every(value => { return arr.indexOf(value) !== -1; }); console.log(multipleExist); // ๐Ÿ‘‰๏ธ true

This is very similar to our previous example, however this time we use the Array.indexOf method.

The indexOf method checks if a value is contained in an array and returns the index of the first occurrence of the value in the array.

If the value is not contained in the array, the method returns -1.

If the indexOf method does not return -1, we can conclude that the value is contained in the array.

This approach is definitely not as direct and readable as using the includes method, but if you have to support Internet Explorer, it gets the job done.

Here is a reusable function that implements this approach.

index.js
function multipleExist(arr, values) { return values.every(value => { return arr.indexOf(value) !== -1; }); } // ๐Ÿ‘‡๏ธ true console.log(multipleExist([1, 2, 3], [1, 2])); // ๐Ÿ‘‡๏ธ false console.log(multipleExist([1, 2, 3], [4, 2]));

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee