JavaScript: Check if Multiple Values Exist in Array

Introduction

In this Byte we'll see several methods to find multiple elements in an array. By the end, you'll know how to do it with methods like include(), some(), and every().

Why Check for Multiple Values in an Array?

When using arrays, there are times when we need to check if certain values exist in an array. This could be for any number of reasons, like filtering data or simply checking conditions in your code.

For example, let's say you're building a movie recommendation engine. You have an array of all the movies a user has watched, and an array of unwatched movies. Now, to recommend a new movie, you'd want to check the movies array for the movies that have been watched so that you can then only recommend new movies.

In the next few sections we'll show the different methods you can use to achieve this.

Using the includes() Method

The includes() method is a built-in JavaScript method that checks if a specific element exists in an array. It returns true if the element is found, and false otherwise.

Here's an example:

let fruits = ['apple', 'banana', 'cherry', 'date'];

console.log(fruits.includes('banana')); // true
console.log(fruits.includes('grape')); // false

However, includes() only checks for a single value. To check for multiple values, we can use a simple for loop:

let fruits = ['apple', 'banana', 'cherry', 'date'];
let checkFruits = ['banana', 'date', 'grape'];

for (let i = 0; i < checkFruits.length; i++) {
    console.log(fruits.includes(checkFruits[i]));
}
Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

Here, we loop over the checkFruits array and checking if each fruit exists in the fruits array. The output will be true for 'banana' and 'date', and false for 'grape'.

Using the some() Method

The some() method is another method that tests whether at least one element in the array passes the test given by the provided function. It returns true if at least one element passes the test, and false otherwise.

We can use the some() method to check if multiple values exist in an array:

let fruits = ['apple', 'banana', 'cherry', 'date'];
let checkFruits = ['banana', 'date', 'grape'];

let result = checkFruits.some(fruit => fruits.includes(fruit));
console.log(result); // true

In this code, we're using the some() method on the checkFruits array. The function we gave checks if each fruit exists in the fruits array using the includes() method. The some() method returns true because 'banana' and 'date' exist in the fruits array, even though 'grape' does not. All it needs is for one item to be true for the entire function to returnt true.

Using the every() Method

The every() method in JavaScript is another useful method that can be used to check if all elements in an array pass a test from our function. Let's see how we can use this method to check if multiple values exist in an array.

let fruits = ['apple', 'banana', 'cherry', 'date'];
let checkFruits = ['banana', 'date', 'grape'];

let result = checkFruits.every(fruit => fruits.includes(fruit));
console.log(result); // false

Similar to our previous examples, this will check if the items in checkFruits exist in fruits. However, since we use the every() method, it will only return true if all items from checkFruits exist in fruits, which in this case is false.

Conclusion

In this Byte, we've shown different methods to check if multiple values exist in an array in JavaScript. We've seen how we can use the includes() method, some() method, and the every() method to check for multiple values.

Last Updated: September 15th, 2023
Was this helpful?

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms