JavaScript
Array Methods
includes()
Coding Tutorial
JavaScript Tips

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In JavaScript, checking whether an array contains a specific value is a common task that can be achieved using several methods, each appropriate for different scenarios and browser support considerations. This article covers the most widely-used methods, including Array.prototype.includes(), Array.prototype.indexOf(), Array.prototype.find(), and Array.prototype.some(). Each approach has its use cases and considerations that we will discuss below.

Methods to Check If an Array Includes a Value

1. Array.prototype.includes()

The includes() method was introduced in ECMAScript 2016 (ES7) and provides a straightforward way to determine if an array includes a certain value. It returns a boolean value.

Syntax:

javascript
array.includes(valueToFind, startIndex)
  • valueToFind: The value to search for within the array.
  • startIndex (optional): The position in the array at which to begin searching. If not provided, the search starts at index 0.

Example:

javascript
const fruits = ['apple', 'banana', 'mango'];
const hasBanana = fruits.includes('banana'); // true
const hasCherry = fruits.includes('cherry'); // false

Considerations:

  • includes() performs a strict equality check (===) when searching for the value.
  • This method is not supported in some very old browsers such as Internet Explorer.

2. Array.prototype.indexOf()

Before includes(), indexOf() was commonly used to check the existence of elements in an array. This method returns the index of the first occurrence of the value in the array, or -1 if it is not present.

Syntax:

javascript
array.indexOf(searchElement, fromIndex)
  • searchElement: The item to search for.
  • fromIndex (optional): The position in the array at which to begin searching. Defaults to 0.

Example:

javascript
const numbers = [1, 2, 3, 4, 5];
const index = numbers.indexOf(3); // 2
const isPresent = index !== -1; // true

Considerations:

  • indexOf() also uses strict equality (===) for comparison.
  • The method can be used in all modern browsers, including older ones like Internet Explorer.

3. Array.prototype.find()

The find() method returns the value of the first element in the provided array that satisfies the provided testing function. It is particularly useful if you need to work with objects or more complex conditions.

Syntax:

javascript
array.find(callback(element[, index[, array]])[, thisArg])
  • callback: Function to execute on each value in the array, taking three arguments:
    • element: The current element being processed.
    • index (optional): The index of the current element.
    • array (optional): The array find() was called upon.
  • thisArg (optional): An object to which the this keyword can refer in the callback function.

Example:

javascript
1const users = [
2  { id: 1, name: 'John' },
3  { id: 2, name: 'Paul' },
4];
5
6const user = users.find(u => u.name === 'Paul'); // { id: 2, name: 'Paul' }
7const isUserPresent = user !== undefined; // true

Considerations:

  • find() stops executing once the callback returns a truthy value.
  • It’s supported in most modern browsers but not in older versions like Internet Explorer.

4. Array.prototype.some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a boolean value, making it suitable if you only need to know if at least one occurrence exists.

Syntax:

javascript
array.some(callback(element[, index[, array]])[, thisArg])
  • Same arguments as find().

Example:

javascript
const people = ['Alice', 'Brian', 'Charlie'];
const hasBrian = people.some(name => name === 'Brian'); // true

Considerations:

  • Once a truthy value is encountered, some() short-circuits and stops processing.
  • Widely supported across modern browsers.

Summary

MethodReturn TypeUses Strict EqualityShort-circuitsSupport
includes()BooleanYesNoModern browsers (not IE)
indexOf()Integer/-1YesNoAll browsers including IE
find()Value/undefinedDepends on callbackYesModern browsers (not IE)
some()BooleanDepends on callbackYesModern browsers

Additional Considerations

  • Performance: If you're dealing with very large arrays and performance is a concern, it's crucial to choose a method that short-circuits (find() or some()) if possible.
  • Polyfills: If you need to support environments where certain methods are not available, consider using polyfills provided by libraries such as core-js.

This overview of how to check if an array includes a value in JavaScript should offer a clear idea of the potential methods available, along with their appropriate use cases and browser support considerations.


Course illustration
Course illustration

All Rights Reserved.