Skip to main content

How to Check if an Array Contains Multiple Specific Values in JavaScript

A common programming task is to verify that an array contains a specific set of required values. For instance, you might need to check if a user's permissions array includes all necessary roles, or if a product's tags contain a required set of keywords. You don't need to know where the values are, just that they all exist somewhere in the array.

This guide will teach you the most effective and modern methods for solving this problem. You will learn the highly readable approach using every() and includes(), and a more performant alternative using a Set for large arrays.

The Core Method (Most Readable): every() with includes()

This is the most modern, concise, and intuitive way to check if an array contains all elements from another. It combines two powerful array methods that read almost like plain English.

The logic:

  1. Use the Array.prototype.every() method to iterate over your array of required values.
  2. For each requiredValue, use the Array.prototype.includes() method to check if it exists in the main array.
  3. The every() method will automatically stop and return false as soon as it finds a required value that is missing. If all values are found, it returns true.

Problem: we need to check if our main array contains all the specified required values.

// Problem: Does 'data' contain all the elements from 'requiredValues'?
const data = ['one', 'two', 'three', 'four'];
const requiredValues1 = ['one', 'two']; // Should be true
const requiredValues2 = ['one', 'five']; // Should be false

Solution:

function containsAll(mainArray, requiredValues) {
return requiredValues.every(value => mainArray.includes(value));
}

// Example Usage:
const data = ['one', 'two', 'three', 'four'];
const required1 = ['one', 'two'];
const required2 = ['one', 'five'];

console.log(containsAll(data, required1)); // Output: true
console.log(containsAll(data, required2)); // Output: false
note

This approach is highly favored for its clarity. The code directly translates the question: "Does every required value exist in the main array?"

The Most Performant Method (for Large Arrays): Using a Set

While the first method is very readable, its performance can suffer if the mainArray is large. For every element in requiredValues, the includes() method has to search through the entire mainArray.

A more performant solution is to first convert the mainArray into a Set. Checking for an element's existence in a Set using set.has() is significantly faster (O(1) on average) than searching an array (O(n)).

Solution:

function containsAll(mainArray, requiredValues) {
const mainSet = new Set(mainArray);
return requiredValues.every(value => mainSet.has(value));
}

// Example Usage:
const data = ['one', 'two', 'three', 'four'];
const required = ['one', 'two'];

console.log(containsAll(data, required)); // Output: true
note

While the difference is negligible for small arrays, this Set-based approach is much more efficient for arrays containing thousands of items or more and is a common optimization.

An Alternative: The Traditional for...of Loop

Before functional methods like every() were common, this problem was solved with a standard for loop. This approach is still perfectly valid, efficient, and can be easier to understand for those who prefer an imperative style.

The logic:

  1. Loop through each value of the requiredValues array.
  2. Inside the loop, check if the mainArray does not include that value.
  3. If you find a value that is missing, you know the answer. Return false immediately and exit the loop.
  4. If the loop completes without finding any missing values, it means all were present, so you can return true.

Solution:

function containsAll(mainArray, requiredValues) {
for (const value of requiredValues) {
if (!mainArray.includes(value)) {
return false; // Found a required value that is missing
}
}
return true; // All required values were found
}

// Example Usage:
const data = ['one', 'two', 'three', 'four'];
const required = ['one', 'five'];

console.log(containsAll(data, required)); // Output: false
note

This method is also very efficient because the return false provides "short-circuiting" behavior, stopping the loop as soon as the result is known.

Conclusion

Verifying that an array contains multiple specific values is a common task with several clean solutions in JavaScript.

  • The required.every(val => main.includes(val)) method is the most concise and readable solution, making it the best choice for most situations.
  • For large arrays where performance is a concern, converting the main array to a Set first provides the most efficient solution.
  • A traditional for...of loop offers an excellent imperative alternative that is also highly efficient and easy to understand.