Register Login

Check if Array Contains a Value or Element in JavaScript

Updated Nov 07, 2022

JavaScript provides many different methods and ways that allow users to check whether an array contains a value or element. Users can still check with the Array.indexOf() or for loop method.

Despite that, ES6 has added many more beneficial JavaScript methods to check an array and encounter what users are looking for; with more efficiency and accuracy. This article will discuss all the different methods to check if a JavaScript array contains a value or not.

What are arrays in JavaScript?

An array is a collection of data of the same data types. When performing JavaScript arrays, a common question arises regarding how users can empty an array and clear all the elements. In the following section, we will discuss the multiple ways to check whether a JS array contains the given item or not.

Methods to check whether an array contains values or elements:

Method 1: Using the include() method to check an array:

The includes() is a method that is part of ES6. Besides strings, users can use the includes() method with other different primitive data types. They can use this method to specify whether an array has a selected number of values or elements.

The includes() method will return true if the JS array contains values or elements. If not, it will return false. The method will return output in a simple boolean value as includes() method is excellent for checking whether the value exists or not.

The include() method checks the entire JS array by default. But users can also add the starting index as the second parameter to start the check from a particular index.

The include() method is case-sensitive.

Code Snippet:

const a = ['red', 'green', 'blue'];
const b = a.includes('red');
console.log(b); // It will return true

const c = a.includes('black');
console.log(c); // It will return false 

Output:

Run Code Snippet

Explanation:

Since, in the above example, the JS array contains the red color, the includes() method first returns true, and when we call the includes() method with a value that does not exist in the JS array, it returns false.

Another example of the includes() method with its properties:

const a = ['Red', 'GREEN', 'Blue'];
const b = a.map(e => e.toLocaleLowerCase()).includes('green');
console.log(b); // It will return true

const c = a.map(e => e.toLocaleLowerCase()).includes('black');
console.log(c); // It will return false

Output:

Run Code Snippet

Method 2: Using the indexOf() method to check an array:

It is the simplest and fastest way to check whether a JS array contains an item or not. The JavaScript Array.indexOf() method checks a JS array for a given value or item, and if it finds the item in the array, it returns its index.

If the JavaScript array contains no such values or items, the method returns -1. The JavaScript Array.indexOf() starts checking a JS array from the start and stops at the end of the array. But users can also add the starting index as the second parameter to start the check from a particular index.

Code Snippet:

var a = ["Abhishek", "Biraj", "Mahi", "Tamana", "Sahir"];
    // This will check if the given value exists in the "a" array
    if(a.indexOf("Sahir") !== -1){
        alert("The value is present in the array.!")
    } else{
        alert("The value is not present in the array.!")
    }
    if(a.indexOf("Dhiraj") !== -1){
        alert("The value is present in the array.!")
    } else{
        alert("The value is not present in the array.!")
    }

Output:

Run Code Snippet

Explanation:

The method returns the index position of the item given in the if-else block. Again, when we passed an item not present in the JS array, it returned and executed the else block of the code.

Method 3: Using the find() method to check the array:

The Array.find() method checks and finds the items and returns if it founds them. If it finds no value, it returns undefined.

Code Snippet:

var a = [1, 2, 3, 4, 5];
// The method will return the element > 10
 var found = a.find(function (element) {
  return element > 2;
 });

// The method will return the desired value
 console.log(found);

Output:

Run Code Snippet

Explanation:

In the above example, we use the find() function to count the number of elements in the array greater than 2.

Method 4: Using the some() method to check the array:

The working of the some() method is almost the same as that of the find(). But the some() method returns a boolean value "true" if it checks and finds the value is present in the array else returns false.

Code Snippet:

const a = [12, 24, 56, 30, 26, 31, 20, 43];

// The method checks whether the value is even
const b = (element) => element == 24;
console.log(a.some(b));

Output:

Run Code Snippet

Explanation:

In the above example, we use some function to check if the array contains an element equal to 24 or not.

Conclusion:

In this article, users can go through four primary methods of checking whether an array contains a value or not. If users want to find the position of the item in the JS array, they can use the indexOf() method.

If they like to know if a given item exists or not, they can use the includes() method. Finally, if users want to match or find an item from the input array, they can use the find() or the some() method.


×