The includes()
method, introduced in ES6, determines whether an array includes a certain value among its entries, returning true
or false
as appropriate.
Example 1: Basic Check for Inclusion
// An array of numbers.
const numbers = [1, 2, 3, 4, 5];
// Check if the array includes the number 3.
const hasThree = numbers.includes(3);
// The boolean result is logged.
console.log(hasThree); // Output: true
Explanation: This is the most straightforward use of includes()
, providing a clear and readable way to check for the presence of an element.
Example 2: Checking for a Non-existent Element
// An array of strings.
const pets = ['cat', 'dog', 'hamster'];
// Check if the array includes 'bird'.
const hasBird = pets.includes('bird');
// The result is logged to the console.
console.log(hasBird); // Output: false
Explanation: If the element is not found, includes()
returns false
, making conditional logic simple and intuitive.
Example 3: Specifying a fromIndex
// An array of characters.
const letters = ['a', 'b', 'c', 'd', 'a', 'b'];
// Check if 'a' is included, starting the search from index 4.
const includesAFromIndex4 = letters.includes('a', 4);
// The result is logged.
console.log(includesAFromIndex4); // Output: true
Explanation: Similar to indexOf()
, includes()
can take a fromIndex
to start the search from a specific position in the array.
Example 4: includes()
vs. indexOf()
for Existence Checks
// An array of numbers.
const numbers = [10, 20, 30];
// Using indexOf()
const hasTwentyWithIndexOf = numbers.indexOf(20) !== -1;
// Using includes()
const hasTwentyWithIncludes = numbers.includes(20);
// Both results are logged.
console.log(hasTwentyWithIndexOf); // Output: true
console.log(hasTwentyWithIncludes); // Output: true
Explanation: While indexOf()
can be used to check for existence, includes()
is often preferred for its more readable and explicit syntax for this purpose.
Example 5: Case-Sensitivity in includes()
// An array of strings.
const words = ['Apple', 'apple', 'APPLE'];
// Check for the lowercase 'apple'.
const hasLowercaseApple = words.includes('apple');
// The boolean result is logged.
console.log(hasLowercaseApple); // Output: true
Explanation: The includes()
method is case-sensitive, meaning 'Apple'
and 'apple'
are considered different values.
Example 6: includes()
with NaN
// An array containing NaN.
const values = [1, NaN, 3];
// Check if the array includes NaN.
const hasNaN = values.includes(NaN);
// The result is logged.
console.log(hasNaN); // Output: true
Explanation: A key advantage of includes()
over indexOf()
is its ability to correctly find NaN
(Not-a-Number) values within an array. indexOf()
would return -1 in this case.
Example 7: Using includes()
in a Filter Function
// Two arrays of numbers.
const allowedNumbers = [1, 3, 5, 7, 9];
const allNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Filter allNumbers to only include numbers present in allowedNumbers.
const filteredNumbers = allNumbers.filter(num => allowedNumbers.includes(num));
// The filtered array is logged.
console.log(filteredNumbers); // Output: [1, 3, 5, 7, 9]
Explanation: This example demonstrates how includes()
can be powerfully combined with other array methods like filter()
to perform more complex data manipulations.