How to check if an array includes a value in JavaScript?

Arrays are a fundamental data structure in JavaScript, and often we need to check if a specific value is present in an array. In this article, we will explore different approaches to solve this problem in a concise and efficient way.

Method 1: Using Array.prototype.includes()

One of the simplest and most efficient ways to check if an array includes a value in JavaScript is by using the includes() method, introduced in ECMAScript 2016. This method returns true if the array contains the specified value, and false otherwise.


            const array = [1, 2, 3, 4, 5];
            const value = 3;
            
            if (array.includes(value)) {
                console.log('The array includes the value');
            } else {
                console.log('The array does not include the value');
            }
        

In this example, we define an array [1, 2, 3, 4, 5] and a value 3. We then use the includes() method to check if the array includes the value. The console will log 'The array includes the value' since the value 3 is present in the array.

It's important to note that the includes() method performs a strict equality comparison (===) to determine if a value is present in the array. This means that it checks both the value and data type.

Method 2: Using Array.prototype.indexOf()

Another approach to check if an array includes a value is by using the indexOf() method. This method returns the index of the first occurrence of a specified value in an array. If the value is not found, it returns -1.


            const array = [1, 2, 3, 4, 5];
            const value = 3;
            
            if (array.indexOf(value) !== -1) {
                console.log('The array includes the value');
            } else {
                console.log('The array does not include the value');
            }
        

Similar to the previous example, we use an array [1, 2, 3, 4, 5] and a value 3. We then find the index of the value using the indexOf() method and check if it is not equal to -1. If the condition is true, the array includes the value.

It's important to note that the indexOf() method performs a strict equality comparison (===) to determine if a value is present in the array.

Method 3: Using Array.prototype.find()

In addition to includes() and indexOf(), JavaScript arrays also provide the find() method which returns the first element in the array that satisfies a provided testing function. If no element is found, it returns undefined.


            const array = [1, 2, 3, 4, 5];
            const value = 3;
            
            const result = array.find(element => element === value);
            
            if (result !== undefined) {
                console.log('The array includes the value');
            } else {
                console.log('The array does not include the value');
            }
        

In this example, we define an array [1, 2, 3, 4, 5] and a value 3. We then use the find() method with an arrow function that checks if each element is equal to the value. If a match is found, the find() method returns the element.

Method 4: Using a for loop

If you need to support older browsers that do not have the includes() or indexOf() methods, you can use a simple for loop to iterate over the array and check if each element matches the value.


            const array = [1, 2, 3, 4, 5];
            const value = 3;
            
            let includes = false;
            
            for (let i = 0; i < array.length; i++) {
                if (array[i] === value) {
                    includes = true;
                    break;
                }
            }
            
            if (includes) {
                console.log('The array includes the value');
            } else {
                console.log('The array does not include the value');
            }
        

In this example, we define an array [1, 2, 3, 4, 5] and a value 3. We initialize a variable includes with false and then iterate over the array using a for loop. Inside the loop, we check if each element is equal to the value and set includes to true if a match is found. We also use the break statement to exit the loop as soon as a match is found.

Comparing the Methods

Now that we have explored different methods to check if an array includes a value in JavaScript, let's compare their efficiency and trade-offs.

The includes() method is the simplest and most concise way to check if an array includes a value. It has a time complexity of O(n), where n is the length of the array. This means that it iterates over the entire array until a match is found or the end of the array is reached.

The indexOf() method is similar to includes() in terms of time complexity (O(n)). However, it returns the index of the value instead of a boolean result. If you only need to check if the value is present in the array, the includes() method is more appropriate.

The find() method is useful if you need to find the element in the array that matches a certain condition. However, it has a time complexity of O(n), which means that it may not be the most efficient option if you only need to check if the array includes the value.

The for loop method has a similar time complexity of O(n) as it also iterates over the entire array. However, it requires more code and is less concise compared to the other methods.

Generally, it is recommended to use the includes() method if you are targeting modern browsers and can rely on ECMAScript 2016 features. If you need to support older browsers, you can use the for loop method.