Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › includes()

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.

Input:

                
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
searchElementanyThe value to search for.
fromIndexnumberOptional. Where to start. Negative counts from end.
1

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!');
}
2

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).

3

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
4

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
5

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
includesBooleanSimple value existence (NaN-safe)
indexOfNumber (-1 if missing)You need the position
someBooleanComplex condition (objects)
findElement or undefinedYou need the matching element

⚠️ Common Mistakes

❓ 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.

🎯 What's Next?

← All JavaScript Reference