JavaScript includes() Method for Array Searching

The JavaScript includes() method determines whether or not an array contains a specific string, returning a boolean value true if the array contains the string, and false if the array does not contain the string.

This article will step you through how to implement the includes() method into your website or web application by exploring a few real-world scenarios.

The JavaScript includes() Method Defined

The includes() method is defined as follows:

array.includes(string, start)
  • The string parameter is required and is the string or value, that will be searched throughout each array index.
  • The start parameter is optional, with a default value of 0, the first value within an array. This is the index in the array where the search will begin.

A Few Examples

Here are some quick examples to help you fully understand the logic behind the includes() method:

var fruits = ["Apples", "Bananas", "Mangos", "Oranges"];

console.log(fruits.includes("Oranges"));
// true

Here, we're creating an array with four strings, each containing a type of fruit. We're then outputting the result to the console. The output in this example is true because the string "Oranges" exists in the fruits array.

When searching for a string that does not exist in the array, the result will return false:

console.log(fruits.includes("Pears"));
// false

What Happens When the start Value Is Less Than Zero

If the start value is negative, the computed index is calculated by adding the array length to the specified start value. Here's an example:

var array = ["a", "b", "c"];

array.includes("a", -10); // true
array.includes("b", -10); // true
array.includes("c", -10); // true
array.includes("a", -2); // false

In this example, the array length is 3. And, for the first three includes() checks, the start value is -10:

3 + (-10) = -7

Meaning the actual start value is -7, which is smaller than the length of the array. Here, true is returned since the entire array is searched.

On the last line, the array length is still 3, but the start value is -2:

3 + (-2) = 1

Now we're starting at array index 1, where b is located and searching through to the end of the array. Since a is at index 0 of the array, the string is not found and the method returns false.

Other Useful Tidbits

  • Comparison strings and characters are always case-sensitive. This means if the array contains the word "Oranges" but we're searching for text "oranges", the result will be false because the case is different between both occurrences of the word.
  • This method uses the sameValueZero() operation to determine whether or not the given string is found within the array.
The sameValueZero() operation accepts two parameters, x and y, and compares those two values returning true if the values match, and false if the values do not match.

Browser Support

Support is provided by most modern browsers and versions. The only exceptions at the time of this writing are all versions of Internet Explorer and Opera.

You can view all supported browsers and versions by clicking the button below:

Browser Support

Conclusion

The JavaScript includes() method provides a clean, easy to use solution for searching for specific strings within arrays by returning a boolean value, letting you know easily if the string exists or not.

Last Updated: December 17, 2020
Created: October 20, 2020