Sometimes, we want to check whether multiple values exist within an JavaScript array.
In this article, we’ll look at how to check whether multiple values exist within an JavaScript array.
How to check whether multiple values exist within an JavaScript array?
To check whether multiple values exist within an JavaScript array, we use the array every
and includes
methods.
For instance, we write
const containsAll = arr1.every((i) => arr2.includes(i));
to call every
with a function that returns whether item i
in arr
is in the array arr2
with includes
.
If every element of arr1
is in arr2
, then true
is returned.
Conclusion
To check whether multiple values exist within an JavaScript array, we use the array every
and includes
methods.