Check If Multiple Values Exist In An Array In JavaScript

To check if multiple values exist in an array in JavaScript –

  1. Use the every() method to iterate over the array.
  2. In each iteration, check if the values are present in the array.
  3. If all the values are present in the array, the every() method returns true, else it will return false.
check if multiple values exist in an array in JavaScript

Check If Multiple Values Exist In An Array In JavaScript

Example

Check if the value “3” and “4” exist in the array:

var arr = [“1”, “2”, “3”, “4”, “5”];

var values = [“3”, “4”];

function checkValues(arr, values) {

  return values.every(function(v) {

     return arr.indexOf(v) >= 0;

});

}

console.log(checkValues(arr, values)); //true

In the above code, the every() method iterates over the values array.

In each iteration, it checks if the current value is present in the arr array.

If all the values are present in the arr array, it returns true, else it returns false.

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

If all the values are not present in the arr array, it returns false.

For example, if the value “6” is not present in the arr array, it would return false.

Check if the value “3”, “4”, and “6” exist in the array:

var arr = [“1”, “2”, “3”, “4”, “5”];

var values = [“3”, “4”,”6″];

function checkValues(arr, values) {

   return values.every(function(v) {

     return arr.indexOf(v) >= 0;

});

}

console.log(checkValues(arr, values)); //false

In the above code, the value “6” is not present in the arr array, hence it would return false.

You can also use the includes() method to check if multiple values exist in an array.

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Example

Check if the value “3” and “4” exist in the array:

var arr = [“1”, “2”, “3”, “4”, “5”];

var values = [“3”, “4”];

function checkValues(arr, values) {

  return values.every(function(v) {

    return arr.includes(v);

});

}

console.log(checkValues(arr, values)); //true

In the above code, the every() method iterates over the values array.

In each iteration, it checks if the current value is present in the arr array using the includes() method.

If all the values are present in the arr array, it returns true, else it returns false.

The includes() method determines whether an array includes a given element, returning true or false as appropriate.

If all the values are not present in the arr array, it returns false.

For example, if the value “6” is not present in the arr array, it would return false.

Check if the value “3”, “4”, and “6” exist in the array:

var arr = [“1”, “2”, “3”, “4”, “5”];

var values = [“3”, “4”,”6″];

function checkValues(arr, values) {

   return values.every(function(v) {

      return arr.includes(v);

});

}

console.log(checkValues(arr, values)); //false

Conclusion

In this article, we looked at how to check if multiple values exist in an array in JavaScript.

We saw how to use the every() and includes() methods to achieve this.

The every() method returns true if all the values are present in the array, else it returns false.

The includes() method determines whether an array includes a given element, returning true or false as appropriate.

We also saw how to use the indexOf() method to achieve the same result as the includes() method.

I hope you found this article helpful. Please feel free to leave your comments and questions below.

Leave a Comment