Definition and Usage
The includes() function for arrays checks whether a given array contains a specified element and returns true if it does, or false otherwise.
Using this function is very useful for checking whether a given array contains a specified element.
If the element being searched for is a string, the comparison is case-sensitive.
Basic Example
const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(3)); // Output: true
console.log(arr.includes(6)); // Output: false
In strings, the includes() function checks whether a specific substring is present in a string.
Syntax
arr.includes(searchElement)
arr.includes(searchElement, fromIndex)
arr is the array to which the includes() function is applied.
Parameters
searchElement |
The element to search for in the array. |
|---|---|
fromIndex |
Optional. A 0-based index indicating the position at which to begin the search.
If omitted, the default value 0 is used. (Array indexes start at 0.)
If a negative value is provided, the search starts at array.length + fromIndex. |
Return Value
The function returns true if the given array contains the specified element.
If it does not contain the specified element, it returns false.
Parameter and Return Value Examples
Distinguishing Data Types
In arrays, the includes() function clearly distinguishes the data type of the element being searched for.
const numbers = [10, 20, 30, 40, NaN];
console.log(numbers.includes(30)); // true
console.log(numbers.includes("30")); // false
console.log(numbers.includes(15)); // false
console.log(numbers.includes(40, 3)); // true
console.log(numbers.includes(20, -2)); // false
console.log(numbers.includes(NaN, -2)); // true
const words = ["apple", "banana", "orange", "grape", "1"];
console.log(words.includes("banana")); // true
console.log(words.includes("pear")); // false
console.log(words.includes("grape", 2)); // true
console.log(words.includes("apple", -4)); // false
console.log(words.includes("banana", -4)); // true
console.log(words.includes(1)); // false
Case Sensitivity of Strings
In arrays, the includes() function performs a case-sensitive comparison when the searched element is a string.
const words = ["apple", "banana", "orange"];
console.log(words.includes("apple")); // true
console.log(words.includes("Apple")); // false
Exact String Matching
When the searched element is a string, the includes() function returns true only if the given string exactly matches an element in the array. If only part of the string matches, it returns false.
const words = ["apple", "banana", "orange"];
console.log(words.includes("banana")); // true
console.log(words.includes("ban")); // false
Checking for the Presence of a Variable
const car = "My car";
const words = ["apple", "banana", car];
console.log(words.includes(car)); // true
console.log(words.includes("My car")); // true
Things to Keep in Mind
There are several important points to be aware of when using the includes() function.
Comparing and Including NaN
In JavaScript, NaN is not equal to itself. That is, NaN === NaN always evaluates to false.
However, when working with arrays, you can use the includes() function to check whether an array contains NaN.
console.log(NaN === NaN); // false
const values = [1, NaN, 3, 4, 5];
console.log(values.includes(NaN)); // true
Using includes() with Sparse Arrays
When searching for undefined in a sparse array, the includes() function returns true.
const arr = [1, , 3];
console.log(arr.includes(undefined)); // true
Compatibility
| Method |
Desktop Chrome
|
DesktopDesktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
includes()
|
47 | 14 | 43 | 9 | 6 |
Specifications
| Specification | |
|---|---|
includes()
|
ECMAScript® 2026 Language Specification #sec-array.prototype.includes |