References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript array.includes() method

Method JavaScript All modern browsers Updated
Quick answer

The includes() method checks whether an array contains a value and returns a clean true or false. ["a", "b"].includes("b") is true. It's the readable, modern replacement for indexOf(x) !== -1, and unlike indexOf() it correctly finds NaN.

Overview

includes() answers a yes/no question: is this value in the array? It returns true or false, which reads far better than the old arr.indexOf(x) !== -1 trick that everyone used before it existed. Use it for membership checks — is this tag selected, is this id already in the list, did the user pick this option.

It compares with the same strict equality as ===, with one welcome exception: it can find NaN, which indexOf() never could. You can pass a second argument to say which index to start searching from, and a negative number counts back from the end.

The catch to remember is that it checks by value for primitives but by reference for objects. [{id: 1}].includes({id: 1}) is false, because those are two different objects that merely look alike. To find an object by its contents, use find() or some() with a condition instead.

Syntax

const has = array.includes(searchElement)
const has = array.includes(searchElement, fromIndex)

Parameters

The array.includes() method accepts the following parameters.

Parameter Description
searchElement The value to look for, compared with strict equality (and matching NaN).
fromIndex Optional. The index to start from. A negative value counts back from the end.

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const fruits = ['apple', 'banana', 'cherry'];

  document.getElementById('out').textContent =
    'banana? ' + fruits.includes('banana') + '\n' +
    'mango?  ' + fruits.includes('mango');
  // banana? true / mango? false
</script>

Best practices

  • Prefer includes() over indexOf(x) !== -1 — it states the intent and returns a real boolean.
  • Use it for primitives (strings, numbers, booleans); for objects, use find() or some() to match by contents.
  • Reach for includes() when you only need yes/no — if you need the position, use indexOf().
  • It's case-sensitive for strings; normalize case first if you want a loose match.

Frequently asked questions

How do I check if an array contains a value?
Use arr.includes(value), which returns true or false.
What is the difference between includes() and indexOf()?
includes() returns a boolean and can find NaN; indexOf() returns a position (or -1) and cannot find NaN. Use includes() for a presence check.
Why does includes() return false for my object?
Objects are compared by reference, not by contents, so a look-alike object isn't equal. Use find() or some() with a condition instead.
Is array includes() case-sensitive?
Yes, for strings. ["A"].includes("a") is false. Lowercase both sides if you need a case-insensitive check.