The quick way to check if an array contains an element in JavaScript is by using the array.includes() method. E.g., the main_array.includes(“element”) expression returns true or false where “element” is the value to search for.
Apart from the .includes() method, there are alternate ways to check if an array contains an element in JavaScript.
- Using array.indexOf() method
- Using array.some() method
- Using the Array.find() method
- Using the Array.findIndex() method
- Using the Array.filter() method
- Using the Array.reduce() method
- Using a for loop
Method 1: Using Array.includes()
Use the array.includes() method to check if an array contains an element in JavaScript. The array includes() is a built-in function that checks whether an array contains a specified element.
The includes() method restricts whether an array contains a particular element among its entries, returning true or false as appropriate.
Syntax and usage
array.includes(element, start)
Parameters
The element is the required parameter and the element to search for.
The start is an optional parameter whose default value is 0 at which position in the array to start the search.
Return Value
The includes() method returns a boolean value.
Example code
// app.js
let dark = ['Jonas', 'Martha', 'Mikkel', 'Ulrich', 'Adam', 'Eva']
console.log(dark.includes('Adam'))
Output
true
That means Adam is there in the dark array, and it is there. Now, let’s see a condition where the item is not in the array.
// app.js
let dark = ['Jonas', 'Martha', 'Mikkel', 'Ulrich', 'Adam', 'Eva']
console.log(dark.includes('Claudia'))
Output
false
Passing the second argument
A false is returned if the start parameter is greater than or equal to the array’s length. Therefore, the array will not be searched.
// app.js
let dark = ['Jonas', 'Martha', 'Mikkel', 'Ulrich', 'Adam', 'Eva']
console.log(dark.includes('Jonas', 6))
Output
false
In this code, the interpreter will start searching Jonas at index 6, which is not in the array, which is why it returns false. It will only count if you provide the start position; if it finds that element, it will return true.
Passing No Arguments
If you don’t pass any arguments to the includes() function, it will return false.
// app.js
let dark = ['Jonas', 'Martha', 'Mikkel', 'Ulrich', 'Adam', 'Eva']
console.log(dark.includes())
Output
false
We did not pass any argument, so it can’t understand which item we are looking for, so it will return true.
Method 2: Using Array.indexOf() method
The Array.indexOf() method is a built-in JavaScript function used to find the index of the first occurrence of a specified element in an array. It can help you if an array contains an element or not.
const LoU = ['ellie', 'joel', 'anna'];
const index = LoU.indexOf('joel');
console.log(index);
Output
1
You can see that the “anna” element is in the array; that’s why it returns the element’s index. If it returns a -1 index, the element does not exist.
const LoU = ['ellie', 'joel', 'anna'];
const index = LoU.indexOf('riley');
console.log(index);
Output
-1
That means an element does not exist in the array, or the array does not contain an element.
Method 3: Using Array.some() method
The Array.some() method is a built-in function used to check if at least one element in an array passes a test implemented by a provided function.
const LoU = ['ellie', 'joel', 'anna'];
const contains = LoU.some(char => char === 'anna');
console.log(contains);
In this example, the Array.some() method is used to check if the LoU array contains the element ‘anna’.
The some() method takes a callback function that checks each element in the array and returns true if at least one element passes the test and false if none do. In this case, the result is true, indicating that the LoU array contains the element ‘anna’.
If an array does not contain an element, it returns false. If we check for the element “riley”, the Array.some() method returns false.
Method 4: Using Array.find() method
The Array.find() method is a built-in JavaScript function used to retrieve the value of the first element in an array that satisfies a given condition.
const LoU = ['ellie', 'joel', 'anna'];
const contains = LoU.find(char => char === 'ellie');
console.log(contains);
Output
ellie
In this example, the Array.find() method is used to check if the LoU array contains the element ‘ellie’.
The array.find() method accepts a callback function that checks each element in the array and returns the first element that passes the test. In this case, the result is ‘ellie’, suggesting that the LoU array contains the element ‘ellie’.
If the array does not contain the element, the index() method returns undefined.
Method 5: Using Array.findIndex() method
The Array.findIndex() method is a built-in function used to retrieve the index of the first element in an array that satisfies a given condition.
We can use the findIndex() method to check if an array contains an element in JavaScript.
const LoU = ['ellie', 'joel', 'anna'];
const element_index = LoU.findIndex(char => char === 'joel');
console.log(element_index);
Output
1
In this example, the Array.findIndex() method is used to check if the LoU array contains the element ‘joel’.
The findIndex() method takes a callback function that checks each element in the array and returns the index of the first element that passes the test. In this case, the result is 1, suggesting that the element ‘joel’ can be found at index 1 in the LoU array.
If the element does not exist in the array, the findIndex() method returns -1 as an output.
Method 6: Using Array.filter() method
JavaScript array.filter() function creates a new array with all elements that pass a given test.
const LoU = ['ellie', 'joel', 'anna'];
const new_arr = LoU.filter(char => char === 'joel');
console.log(new_arr);
Output
[ 'joel' ]
In the above code example, the Array.filter() method is used to check if the ‘LoU’ array contains the element ‘joel’.
The filter() method accepts a callback function that checks each element in the array and returns a new array with all elements that pass the test. In this case, the result is [‘joel’], suggesting that the “LoU” array contains the element ‘joel’.
If the filter() method returns an empty array, that means an array does not contain the element we are looking for. And that’s how we can use the filter() method.
Method 7: Using Array.reduce() method
JavaScript Array.reduce() method applies a function to each element in an array, reducing the elements to a single value.
const LoU = ['ellie', 'joel', 'anna'];
const bool = LoU.reduce((acc, char) => {
return acc || char === 'joel';
}, false);
console.log(bool);
Output
true
In the code example, the Array.reduce() method checks if the “LoU” array contains the element “joel”.
The reduce() method takes a callback function that checks each element in the array and accumulates the result. In this case, the reduce() method returns true if any of the elements in the “LoU” array is equal to ‘joel’.
Method 8: Using the for loop
A for loop is a typical way to iterate over an array in JavaScript and check if it contains a specific element.
const LoU = ['ellie', 'joel', 'anna'];
let containsEllie = false;
for (let i = 0; i < LoU.length; i++) {
if (LoU[i] === 'ellie') {
containsEllie = true;
break;
}
}
console.log(containsEllie);
Output
true
In this example, the for loop is used to iterate over the “LoU” array and check if any elements are equal to ‘ellie’. If the loop finds an element equal to ‘ellie’, it sets containsEllie to true and exits the loop.
If it does not find the element, it returns false and exits the loop.
Conclusion
To check if an array contains a specific element, use the array includes() function. The includes() method returns true if an array contains an element. Otherwise, it returns false.