Check if Multiple Values exist in Array in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# Check if Multiple Values exist in an Array in JavaScript

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

check if multiple values exist in array

The code for this article is available on GitHub

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

On each iteration, we use Array.includes() method to check if the value is contained in the other array.

index.js
const arr = ['one', 'two', 'three', 'four']; console.log(arr.includes('one')); // ๐Ÿ‘‰๏ธ true console.log(arr.includes('two')); // ๐Ÿ‘‰๏ธ true console.log(arr.includes('abc')); // ๐Ÿ‘‰๏ธ false

If even a single value is not contained in the array, the includes() method returns false, which causes the every method to short-circuit also returning false.

If you need to do this often, define a reusable function.

index.js
function multipleInArray(arr, values) { return values.every(value => { return arr.includes(value); }); } console.log(multipleInArray([1, 2, 3], [1, 3])); // ๐Ÿ‘‰๏ธ true console.log(multipleInArray([1, 2, 3], [1, 10])); // ๐Ÿ‘‰๏ธ false

The multipleInArray function takes an array and a collection of values and checks if the specified values exist in the array.

If the condition is met for all values, the function returns true, otherwise, false is returned.

# Check if multiple values exist in an array using a Set object

This is a three-step process:

  1. Convert the array to a Set object.
  2. Use the every() method to iterate over the array of values.
  3. Use the Set.has() method to check if each value is in the Set.
index.js
function multipleInArray(arr, values) { const set = new Set(arr); return values.every(value => { return set.has(value); }); } // ๐Ÿ‘‡๏ธ true console.log( multipleInArray(['bobby', 'hadz', 'com'], ['bobby', 'hadz']), ); // ๐Ÿ‘‡๏ธ false console.log(multipleInArray([1, 2, 3], [1, 10]));

check if multiple values exist in array using set

The code for this article is available on GitHub

We used the Set() constructor to convert the array to a Set object to remove all the duplicates (if any).

index.js
// ๐Ÿ‘‡๏ธ Set(3) { 1, 2, 3 } console.log(new Set([1, 1, 2, 2, 3, 3]));

Set objects are collections of unique elements, so if any duplicates exist in the array, they get dropped once passed to the Set() constructor.

The next step is to use the Array.every() method to iterate over the array of values.

On each iteration, we use the Set.has() method to check if the current value is contained in the Set object.

The Set.has method returns true if the value is contained in the Set and false otherwise.

# Check if Multiple Values exist in an Array using indexOf()

This is a three-step process:

  1. Use the every() method to iterate over the array of values.
  2. Use the indexOf method to check if each 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 multipleInArray = values.every(value => { return arr.indexOf(value) !== -1; }); console.log(multipleInArray); // ๐Ÿ‘‰๏ธ true

check if multiple values exist in array using indexof

The code for this article is available on GitHub

This is very similar to our previous example. However, this time we used 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 doesn't return -1, then the value is contained in the array.

Here is a reusable function that achieves the same result.

index.js
function multipleInArray(arr, values) { return values.every(value => { return arr.indexOf(value) !== -1; }); } // ๐Ÿ‘‡๏ธ true console.log(multipleInArray([1, 2, 3], [1, 2])); // ๐Ÿ‘‡๏ธ false console.log(multipleInArray([1, 2, 3], [4, 2]));
The code for this article is available on GitHub

The function takes the array and the collection of values as parameters and returns true if all of the values are contained in the array and false otherwise.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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