Open In App

How do I check if an Array includes a value in JavaScript?

Last Updated : 10 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We are going to learn how to check if a value is present in an array or not. We will need the array we want to search through and the target element we are looking for. JavaScript arrays serve as convenient containers for holding lists of elements, accessible through a single variable.

Once we have a target element we can perform various search algorithms to confirm its presence within the array.

Different Approaches to Check if Array Includes the Value

1. Linear Search Algorithm (Naive approach):

In the Linear search algorithm, we compare each element of the array with the target. 8 is part of the num array below.

Javascript
let num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function check(element) {

    for (let i = 0; i < num.length; i++) {
        if (num[i] == element)
            return element + " is present in the array.";

    }
    return element + " is not present in the array.";
}
console.log(check(8));

Output
8 is present in the array.

Time complexity: O(n)

The time complexity for this algorithm is O(n) as we are looping through the array once to check for the given element.

Space complexity: O(1)

The space complexity for this algorithm is O(1) as we are not using any additional space other than the input array.

2. Using indexOf() function:

The indexOf() function returns the index of the target element in the array if it is present and -1 if not present.

For example, 41 is not part of the num array in the code below.

Javascript
let num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let element = 41;
if (num.indexOf(element) > 0)
    console.log(element + " is present.");
else
    console.log(element + " is not present.");

Output
41 is not present.

Time complexity: O(n)
Space complexity: O(1)

3. Binary Search:

The Binary search algorithm works only on sorted arrays and keeps dividing the array into 2 equal halves and works recursively.

Javascript
function bsearch(arr, l, r, x) {
    if (r >= l) {
        let mid = l + Math.floor((r - l) / 2);

        if (arr[mid] == x)
            return mid;

        if (arr[mid] > x)
            return bsearch(arr, l, mid - 1, x);

        return bsearch(arr, mid + 1, r, x);
    }

    return -1;

}

let num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// To check if 85 is present or not
console.log("Is 85 present? " + (bsearch(num, 0, num.length, 85) != -1));

// To check if 1 is present or not
console.log("Is 1 present? " + (bsearch(num, 0, num.length, 1) != -1)); 

Output
Is 85 present? false
Is 1 present? true

Time Complexity: O(log n)
Space Complexity: O(1)

4. Using filter() Method:

The filter() method is used with the array to pull out the desired element from the array. We first create the array and use the filter method on an array with a method that checks element is present or not. If the element is present in the array it returns an array with an element else returns an empty array. 

Javascript
let num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function check(element) {

    let ans = num.filter(x => x == element);
    if (ans.length)
        return element + " is present in the array.";

    return element + " is not present in the array.";
}
console.log(check(81));

Output
81 is not present in the array.

Time Complexity: O(N). Here N is the size of an array. 
Space Complexity: O(1). Because it does not require extra memory. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads