How To Check If Multiple Values Exist In A JavaScript Array

Working with JavaScript arrays often involves checking if specific values exist within the array. In some cases, you might need to check for the existence of multiple values simultaneously.

In this tutorial, we’ll explore different methods for checking if multiple values exist in a JavaScript array, covering various scenarios and use cases.

The Scenario

Let’s start with a scenario where you have an array and want to check if it contains specific values. For example, you have the following array:

const numbers = [10, 20, 30, 40, 50];

You want to determine if this array contains both the values 20 and 40.

All Methods #

Method 1: Using the includes() Method

The includes() method is a built-in array method that allows you to check if an array contains a specific value. To check for multiple values, you can use the includes() method in combination with the logical AND (&&) operator:

const numbers = [10, 20, 30, 40, 50];
const value1 = 20;
const value2 = 40;

const bothValuesExist = numbers.includes(value1) && numbers.includes(value2);

console.log(bothValuesExist); // Output: true

In this example, we use includes() twice to check for the existence of both value1 and value2. The && operator ensures that both conditions must be true for bothValuesExist to be true.

Method 2: Using the every() Method

The every() method checks if all elements in an array satisfy a specified condition. You can use it to check if all the values you’re interested in exist in the array:

const numbers = [10, 20, 30, 40, 50];
const valuesToCheck = [20, 40];

const allValuesExist = valuesToCheck.every(value => numbers.includes(value));

console.log(allValuesExist); // Output: true

Here, we create an array called valuesToCheck containing the values we want to find in the numbers array. Then, we use the every() method to check if all values in valuesToCheck exist in numbers.

Method 3: Using the some() Method

The some() method checks if at least one element in an array satisfies a specified condition. You can use it to check if any of the values you’re interested in exist in the array:

const numbers = [10, 20, 30, 40, 50];
const valuesToCheck = [20, 60];

const anyValueExists = valuesToCheck.some(value => numbers.includes(value));

console.log(anyValueExists); // Output: true

In this example, we create an array called valuesToCheck containing the values we want to find in the numbers array. Then, we use the some() method to check if any value in valuesToCheck exists in numbers.

Method 4: Using a Custom Function

You can also create a custom function that checks for the existence of multiple values in an array. Here’s an example:

function valuesExistInArray(array, valuesToCheck) {
  return valuesToCheck.every(value => array.includes(value));
}

const numbers = [10, 20, 30, 40, 50];
const valuesToCheck = [20, 40];

const bothValuesExist = valuesExistInArray(numbers, valuesToCheck);

console.log(bothValuesExist); // Output: true

In this approach, we define a valuesExistInArray function that takes an array and an array of values to check. The function uses the every() method to check if all values in valuesToCheck exist in the given array.