How do I check if an array contains an element in JavaScript?

To check if an array contains a specified value in JavaScript, you can use the array.includes(), array.indexOf(), array.find(), and array.filter() methods. The array.includes() method allows you to determine if the array contains the element you are looking for and returns the boolean "true" or "false". The array.indexOf() method allows you to determine if the JavaScript array contains the specified element and returns the index of the first match, or "-1". By default, the indexOf() method starts searching at the beginning of the array; to search from the end of the array, you can use the lastIndexOf() method. The array.find() method returns the first element in the array that matches the condition according to the callback function, or "undefined". The array.filter() method allows you to create a new array containing the elements from the original array that match the condition specified in the passed function. In this JavaScript Check If Array Contains Element example, we use the array.includes() method. Below you can see more examples of checking if an array contains an element with a detailed description of each method. Click Execute to run the JavaScript Array Contains Example online and see the result.
How do I check if an array contains an element in JavaScript? Execute
let array = ['Strawberry', 'Grape', 'Orange', 'Cherry'];

console.log(array.includes('Grape'));
Updated: Viewed: 955 times

Using indexOf() to determine if a JavaScript array contains a selected element

The array.indexOf() method in JavaScript returns the index of the first element in the array that matches the search condition, or "-1". The indexOf() method is case sensitive when searching for strings in an array. The indexOf() method takes two arguments; search value and starting index. The search is performed from left to right. By default, the search starts at the first element and ends at the last. If a negative index value is passed, then the starting index is counted from the last element to the left by the specified number of elements (but the search continues from right to left. The main difference between the include() and indexOf() methods is that include() returns a boolean value (true or false) while the indexOf() method returns the first index of the matched element, or -1 (if the element is not found).

JavaScript array.indexOf() Syntax
array.indexOf(searchElement, index)

Where:
  • searchElement: the value to find inside the array
  • index (optional): negative values are allowed. In this case, the starting index from which the search will begin will be calculated using the following formula: length (array length) - index
JavaScript Array Contains Example using array.indexOf()
let array = ['Strawberry', 'Grape', 'Orange', 'Cherry', 'Grape', 'Banana'];

console.log(array.indexOf('Grape'));

// output: 1

If you need to search for the desired element from the end of the array, you can use the array.lastIndexOf() method. The array.lastIndexOf() method returns the last index of the specified value, or -1 if no value was found. By default, the search starts at the last element and ends at the first. A negative index value tells the lastIndexOf() method to start searching for the last element minus the specified number of elements (but still searching from right to left).

JavaScript Array Contains Example using array.lastIndexOf()
let array = ['Strawberry', 'Grape', 'Orange', 'Cherry', 'Grape', 'Banana'];

console.log(array.lastIndexOf('Grape'));

// output: 4

Using includes() to check if a JavaScript array contains a selected element

The array.includes() method in JavaScript checks if an array contains a given element. The includes() method is best for using in "if" conditions. The method returns the boolean value "true" if the element is found, otherwise "false". The include() method is case sensitive when searching for strings in an array.

JavaScript array.includes() Syntax
array.includes(searchElement, index)

Where:
  • searchElement: the value to find inside the array
  • index (optional): an integer specifies the array index at which to start searching for an array element. If the value is not set, the search is performed from the zero index. Negative values are allowed. In this case, the search will start at the index length of the array - the passed value
JavaScript Array Contains Example using array.includes()
let array = ['Strawberry', 'Grape', 'Orange', 'Cherry'];

console.log(array.includes('Grape'));

// output: true

Difference between indexOf() and includes() methods

The main difference between the indexOf() and include() methods is that the indexOf() method returns the index of the found element in the array (or -1), while the include() method returns a boolean true or false. When searching inside an array containing NaN elements, the include() method will return true, and the indexOf() method will return -1.

JavaScript Check Array Contain NaN Example
let arr = [NaN];

console.log(arr.indexOf(NaN));
// output: -1

console.log(arr.includes(NaN));
// output: true

Using the find() method to check if a JavaScript array contains a specified element

The array.find() method returns the first element in the array that satisfies the condition in the passed function, or "undefined". Compared to the array.indexOf() and array.includes() methods, the array.find() method takes a function as a parameter and executes it for each array elements until it returns the boolean "true". When the first element is found, the find() method immediately returns the value without checking the rest of the values in the array.

JavaScript Array Contains Example using array.find()
const arr = [10, 21, 43, 1, 24, 12, 6];
if (arr.find(i => (i === 21))) {
  console.log('Found');
} else {
   console.log('Not Found');
}

// output: Found

Using the filter() method to check if a JavaScript array contains a specified element

The array.filter() method returns an array of elements that match the criteria defined in the function passed as an argument. The filter() method does not modify the original array.

JavaScript array.filter() Syntax
array.filter(function(currentValue, index, array), thisValue)

Where:
  • function: a callback function will be executed once for each element in the array. If something that is not a function object is passed as a method parameter, then a TypeError exception will be raised.
  • thisValue (optional): an object that can be referenced by the "this" keyword inside the function. If thisValue parameter is not passed, "this" will be "undefined"
JavaScript Find Element using filter() method Example
const arr = [21, 43, 23, 1, 34, 12, 8];

if (arr.filter(i => (i === 23)).length) {
  console.log('Found');
} else {
  console.log('Not Found');
}

// output: Found

See also