Skip to main content

How to Check if an Array Contains All Elements of Another Array in JavaScript

A common programming task is to verify that one array is a "subset" of another—that is, to check if a main array contains every single element from a second array. This is essential for tasks like checking if a user has all the required permissions, if a product has all the necessary tags, or if a dataset meets a set of criteria.

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(), a more performant alternative using a Set for large arrays, and the traditional for...of loop for an imperative approach.

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

This is the most modern, concise, and intuitive way to check if one array contains all elements of 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 the array that should be the "subset" (the one with the required elements).
  2. For each element in this subset array, use Array.prototype.includes() to check if it exists in the main "superset" array.
  3. The every() method will automatically stop and return false as soon as it finds an element that is missing. If all elements are found, it will return true.

Problem: we need to check if a user has all the required permissions to perform an action.

// Problem: Does userPermissions contain all requiredPermissions?
const requiredPermissions = ['EDIT', 'DELETE'];
const userPermissions1 = ['READ', 'EDIT', 'DELETE', 'CREATE']; // Has all
const userPermissions2 = ['READ', 'EDIT']; // Is missing 'DELETE'

Solution:

function containsAll(subset, superset) {
return subset.every(element => superset.includes(element));
}

// Example Usage:
const required = ['EDIT', 'DELETE'];
const userPerms1 = ['READ', 'EDIT', 'DELETE', 'CREATE'];
const userPerms2 = ['READ', 'EDIT'];

console.log(containsAll(required, userPerms1)); // Output: true
console.log(containsAll(required, userPerms2)); // Output: false
note

This approach is highly favored for its clarity and conciseness.

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

The every() with includes() method is very readable, but it can be inefficient if the "superset" array is very large. For every element in the subset, the includes() method has to search through the entire superset array.

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

Solution:

function containsAll(subset, superset) {
const supersetSet = new Set(superset);
return subset.every(element => supersetSet.has(element));
}

// Example Usage:
const required = ['EDIT', 'DELETE'];
const userPerms = ['READ', 'EDIT', 'DELETE', 'CREATE'];

console.log(containsAll(required, userPerms)); // 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.

The Traditional Method: Using a 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 programming style.

The logic:

  1. Loop through each element of the "subset" array.
  2. Inside the loop, check if the "superset" array does not include that element.
  3. If you find an element that is missing, you have your answer. Return false immediately and exit the loop.
  4. If the loop completes without finding any missing elements, it means all were present, so you can return true.

Solution:

function containsAll(subset, superset) {
for (const element of subset) {
if (!superset.includes(element)) {
return false; // Found an element that is not in the superset
}
}
return true; // All elements were found
}

// Example Usage:
const required = ['EDIT', 'DELETE'];
const userPerms = ['READ', 'EDIT'];

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

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

Conclusion

Verifying if one array contains all elements of another is a common problem with several clean solutions in JavaScript.

  • The subset.every(el => superset.includes(el)) method is the most concise and readable solution, making it the best choice for most general-purpose use cases.
  • For large arrays where performance is critical, converting the superset 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.