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

In JavaScript, you can check if every element of the first array exists in the second array, in the following ways:

Using Array.prototype.every()

To check if every element of the first array exists in the second array, you can do the following:

  1. Use Array.prototype.every() method to check whether all elements in an array pass the test implemented by the provided function;
  2. Use Array.prototype.includes() (or Array.prototype.indexOf() in the callback of Array.prototype.every() to check if the current element of first array exists in the second array.

For example, you can implement this in the following way:

// ES7+
const arr1 = [ 1, 2, 3 ];
const arr2 = [ 3, 5, 4, 2, 7, 0, 1, 10 ];

const hasAllElems = arr1.every(elem => arr2.includes(elem));

console.log(hasAllElems); // true

If one or more elements do not exist in the second array, then false is returned (as you can see in the example below):

// ES7+
const arr1 = [ 1, 2, 16 ];
const arr2 = [ 3, 5, 4, 2, 7, 0, 1, 10 ];

const hasAllElems = arr1.every(elem => arr2.includes(elem));

console.log(hasAllElems); // false

If you are unable to support ES7, then you can achieve the same in ES5 using Array.prototype.indexOf() (instead of Array.prototype.includes()), for example, like so:

// ES5+
const arr1 = [ 1, 2, 3 ];
const arr2 = [ 3, 5, 4, 2, 7, 0, 1, 10 ];

const hasAllElems = arr1.every(function (elem) {
    return arr2.indexOf(elem) > -1;
});

console.log(hasAllElems); // true

If you are able to support ES6, then you can rewrite the callback to Array.prototype.every() as an arrow function to make it more compact.

Using a Loop

To check if every element of the first array exists in the second array, you can do the following:

  1. Use a loop (such as a for loop) and iterate over the first array;
  2. In each iteration, use Array.prototype.indexOf() (or Array.prototype.includes()) to check if the current element of first array exists in the second array;
  3. Return true from the callback if all elements of first array exist in the second array, otherwise, return false.

For example, you can implement this using a for loop and Array.prototype.indexOf() in the following way:

const arr1 = [ 1, 2, 3 ];
const arr2 = [ 3, 5, 4, 2, 7, 0, 1, 10 ];

let hasAllElems = true;

for (let i = 0; i < arr1.length; i++){
    if (arr2.indexOf(arr1[i]) === -1) {
        hasAllElems = false;
        break;
    }
}

console.log(hasAllElems); // true

This can be especially useful if you are unable to support a minimum ES5 as with the other method.

Using the same code, you can see that if one or more elements do not exist in the second array, then false is returned:

const arr1 = [ 1, 2, 16 ];
const arr2 = [ 3, 5, 4, 2, 7, 0, 1, 10 ];

let hasAllElems = true;

for (let i = 0; i < arr1.length; i++){
    if (arr2.indexOf(arr1[i]) === -1) {
        hasAllElems = false;
        break;
    }
}

console.log(hasAllElems); // false

You can also create a function for this like so:

function arrayContainsAll(needle, haystack) {
    for (let i = 0; i < needle.length; i++){
        if (haystack.indexOf(needle[i]) === -1) {
            return false;
        }
    }

    return true;
}

const result1 = arrayContainsAll([ 1, 2, 3 ], [ 3, 5, 4, 2, 7, 0, 1, 10 ]);
const result2 = arrayContainsAll([ 1, 2, 16 ], [ 3, 5, 4, 2, 7, 0, 1, 10 ]);

console.log(result1); // true
console.log(result2); // false

This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.