Course contentsShow
← JavaScript
Lesson 830 of 2,72319. ArraysFree lesson

includes() for Membership Testing

Check if an array contains a specific value using includes().

What You'll Learn

You'll learn how to quickly check if an array contains a specific value using the includes() method.

The Purpose of includes()

Sometimes you just need a simple yes-or-no answer: "Is this value in my array?" While indexOf() can tell you where something is (or -1 if it's not there), includes() gives you a straightforward true or false.

Think of it like asking a librarian, "Do you have this book?" instead of "Where exactly is this book located?" Sometimes you don't care about the location—you just need to know if it exists.

How includes() Works

The method checks if an array contains a specific value and returns a boolean:

const fruits = ['apple', 'banana', 'orange'];

fruits.includes('banana');  // true
fruits.includes('grape');   // false

Optional Starting Index

You can also tell includes() where to start searching by passing a second argument:

const numbers = [1, 2, 3, 4, 5];

numbers.includes(3);      // true
numbers.includes(3, 3);   // false (starts searching from index 3)

Why Use includes() Instead of indexOf()?

Compare these approaches:

const colors = ['red', 'blue', 'green'];

// Less readable
if (colors.indexOf('blue') !== -1) {
    // Found it
}

// More readable
if (colors.includes('blue')) {
    // Found it
}

The includes() version is clearer and more expressive—your intent is obvious at a glance.

Special Case: NaN

Unlike indexOf(), includes() can correctly find NaN values:

const values = [1, 2, NaN, 4];

values.includes(NaN);   // true
values.indexOf(NaN);    // -1 (can't find it!)

Key Takeaway: Use includes() when you need a simple true/false answer about whether an array contains a value—it's more readable than checking indexOf() !== -1 and handles NaN correctly.