The JavaScript Array.includes() method is used to check if a given array contains a specified element. It returns a Boolean (true or false) based on whether the element is found in the array. This method is case-sensitive and works with both primitive types (like numbers and strings) and object references.
For example, array.includes(valueToFind) is useful when you need to verify the existence of an element in an array.
Syntax
The syntax for the Array.includes() method is:
array.includes(valueToFind, fromIndex)
Parameters
The Array.includes() method accepts two parameters:
- valueToFind (Required): The element to search for in the array.
- fromIndex (Optional): The index at which to start the search. The default is 0. If a negative index is provided, it starts from the end of the array.
Return Value
The method returns a Boolean value:
- true if the array contains the specified element.
- false if the element is not found.
Examples of JavaScript Array.includes() Method
Example 1: Checking for a Value in an Array
You can use includes() to check if a value exists in an array.
const fruits = ['apple', 'banana', 'mango'];
const hasApple = fruits.includes('apple');
console.log(hasApple); // Output: true
Explanation: The includes() method checks if the array fruits contains the element 'apple' and returns true because 'apple' exists in the array.
Example 2: Checking for a Non-Existent Value
If the value does not exist in the array, includes() returns false.
const fruits = ['apple', 'banana', 'mango'];
const hasOrange = fruits.includes('orange');
console.log(hasOrange); // Output: false
Explanation: The method returns false because 'orange' is not present in the fruits array.
Example 3: Using includes() with a Number Array
The includes() method works with numbers as well.
const numbers = [1, 2, 3, 4, 5];
const hasNumber = numbers.includes(3);
console.log(hasNumber); // Output: true
Explanation: The method checks if 3 exists in the numbers array and returns true.
Example 4: Case-Sensitivity in includes()
The method is case-sensitive when working with strings.
const fruits = ['Apple', 'Banana', 'Mango'];
const hasAppleLowerCase = fruits.includes('apple');
console.log(hasAppleLowerCase); // Output: false
Explanation: The method returns false because 'apple' (in lowercase) does not match 'Apple' (with an uppercase “A”) in the fruits array.
Example 5: Using includes() with fromIndex
You can specify the index from which to start searching using the fromIndex parameter.
const numbers = [1, 2, 3, 4, 5];
const hasNumberFromIndex = numbers.includes(3, 3);
console.log(hasNumberFromIndex); // Output: false
Explanation: The method starts searching from index 3, but 3 is located before that, so it returns false.
Example 6: Using includes() with Negative fromIndex
You can provide a negative fromIndex to start the search from the end of the array.
const numbers = [1, 2, 3, 4, 5];
const hasNumberFromNegativeIndex = numbers.includes(4, -2);
console.log(hasNumberFromNegativeIndex); // Output: true
Explanation: The fromIndex of -2 means the method starts searching from the second-to-last element. Since 4 is present at index 3, it returns true.
Example 7: Using includes() with NaN
The includes() method correctly detects NaN (Not-a-Number) in an array, unlike some other methods such as indexOf().
const array = [NaN, 2, 3];
const hasNaN = array.includes(NaN);
console.log(hasNaN); // Output: true
Explanation: The method returns true because NaN is included in the array.