JavaScript Reference
Array.prototype.includes()
Returns true if the array contains a value. NaN-safe.
Last updated: October 2026
Definition: Array.prototype.includes() checks whether an array contains a specific value and returns a boolean. It uses SameValueZero comparison, which means NaN is correctly detected — unlike indexOf().
🎬 Live Example
Five includes patterns. Click any button to see the output.
💡 includes returns a boolean. Use it for existence checks.
Basic Syntax
array.includes(searchElement, fromIndex)
// searchElement — value to look for
// fromIndex — optional starting index (default 0)
// Returns: boolean
🎯 Syntax & Parameters
| Parameter | Type | Description |
|---|---|---|
searchElement | any | The value to search for. |
fromIndex | number | Optional. Where to start. Negative counts from end. |
Basic Existence Check
const fruits = ['apple', 'banana', 'cherry'];
fruits.includes('banana'); // true
fruits.includes('grape'); // false
// Perfect for if conditions
if (fruits.includes('apple')) {
console.log('We have apples!');
}
NaN-Safe Comparison
const nums = [1, 2, NaN, 4];
nums.includes(NaN); // true ✅
nums.indexOf(NaN); // -1 ❌
Why? includes uses SameValueZero (NaN === NaN). indexOf uses strict equality (NaN !== NaN).
Using fromIndex
const arr = ['a', 'b', 'c', 'a'];
arr.includes('a'); // true (found at 0)
arr.includes('a', 1); // true (found at 3)
arr.includes('a', 4); // false (past end)
// Negative counts from end
arr.includes('a', -1); // true
String includes()
const greeting = 'Hello World';
greeting.includes('World'); // true
greeting.includes('world'); // false (case-sensitive)
greeting.includes('Hello', 0); // true
greeting.includes('Hello', 1); // false
// Case-insensitive check
greeting.toLowerCase().includes('world'); // true
Real-World: Whitelist Check
const allowedRoles = ['admin', 'editor', 'viewer'];
const userRole = 'editor';
if (allowedRoles.includes(userRole)) {
grantAccess();
} else {
denyAccess();
}
// In objects, use .some() instead:
const users = [{id:1}, {id:2}];
users.some(u => u.id === 1); // true ✅
users.includes({id:1}); // false ❌
🎯 includes vs indexOf vs some vs find
| Method | Returns | Use when |
|---|---|---|
includes | Boolean | Simple value existence (NaN-safe) |
indexOf | Number (-1 if missing) | You need the position |
some | Boolean | Complex condition (objects) |
find | Element or undefined | You need the matching element |
⚠️ Common Mistakes
- Using includes on objects. It compares by reference. Use
some()for property checks. - Expecting case-insensitive string matching. Normalize with
toLowerCase(). - Confusing fromIndex with endIndex. Search starts at fromIndex and continues to the end.
- Assuming -0 and +0 differ. They're treated as equal (SameValueZero).
- Using includes in a hot loop. It's O(n). For sets, use
Set.has()for O(1). - Mixing includes with indexOf for boolean. Prefer includes for readability.
❓ Frequently Asked Questions
What does Array.includes() do in JavaScript?
Array.includes() returns true if the array contains a given value, and false otherwise. It uses SameValueZero comparison, meaning NaN is found and -0 equals +0.
What is the difference between includes and indexOf?
includes returns a boolean and correctly finds NaN. indexOf returns an index (or -1) but fails to detect NaN. Use includes for existence checks; use indexOf when you need the position.
Can includes search from a specific index?
Yes. Pass a second argument: arr.includes(value, fromIndex). Negative indices count from the end. Search starts at that index or later.
Does includes work on strings too?
Yes. String.prototype.includes(substring) returns true if the string contains the substring. It also accepts a position argument.
Does includes work on objects?
Only by reference. includes({a:1}) will not find an object that looks identical; it must be the exact same object reference.