Skip to content

includes

This lesson explains includes with clear examples, use cases, and best practices so you can use modern JavaScript confidently in real projects.

ES6+ includes Overview

The includes() method checks whether an array or string contains a specific value. It returns a boolean, which makes membership checks easier to read than older patterns such as indexOf() !== -1.

Array.prototype.includes() was added in ES2016 and is commonly used for roles, tags, permissions, selected values, and simple search checks in modern JavaScript applications.

Feature Description
Introduced In ES2016 (ES7)
Array Method array.includes(value)
String Method string.includes(searchText)
Returns true or false
Optional Argument fromIndex
Common Uses Membership checks, filters, validation, search

Basic Array includes Example

const roles = [
  "admin",
  "editor",
  "viewer"
];

console.log(
  roles.includes("editor")
);

console.log(
  roles.includes("guest")
);

includes() returns true when the value exists in the array and false when it does not.

How includes Works

JavaScript compares the search value with array elements using the SameValueZero algorithm, which treats NaN as equal to NaN.

Step Description Example
Choose Array Start with the collection to search. roles
Pass Value Provide the value to look for. "editor"
Compare Items Check each element for a match. SameValueZero
Return Boolean Stop when a match is found. true
Use Result Apply in conditions and filters. if (...)

includes with fromIndex

const numbers = [
  10, 20, 30, 20, 40
];

console.log(
  numbers.includes(20)
);

console.log(
  numbers.includes(20, 2)
);

console.log(
  numbers.includes(20, -2)
);

The optional fromIndex argument tells includes() where to begin searching in the array.

includes and NaN

const values = [
  1,
  NaN,
  3
];

console.log(
  values.indexOf(NaN)
);

console.log(
  values.includes(NaN)
);

Unlike indexOf(), includes() can detect NaN values in an array.

String includes Example

const email =
  "alex@example.com";

console.log(
  email.includes("@")
);

console.log(
  email.includes(".com")
);

console.log(
  email.includes("test")
);

Strings also have an includes() method for checking whether a substring exists inside text.

Using includes in Conditions

const permissions = [
  "read",
  "write",
  "delete"
];

function canDelete(userPermissions) {
  return userPermissions.includes(
    "delete"
  );
}

console.log(
  canDelete(permissions)
);

console.log(
  canDelete(["read"])
);

Boolean results from includes() work naturally in if statements, guards, and permission checks.

Filter with includes

const products = [
  "Keyboard",
  "Mouse",
  "Monitor",
  "Webcam"
];

const selected = ["Mouse", "Monitor"];

const filtered =
  products.filter((item) =>
    selected.includes(item)
  );

console.log(filtered);

includes() is often combined with filter() to keep only items that exist in another list.

Object References in includes

const userA = { id: 1 };
const userB = { id: 2 };

const users = [userA, userB];

console.log(
  users.includes(userA)
);

console.log(
  users.includes({ id: 1 })
);

includes() checks reference equality for objects, not deep value equality. A new object with the same properties will not match.

includes vs indexOf

const tags = [
  "javascript",
  "es6",
  "tutorial"
];

// Old pattern
console.log(
  tags.indexOf("es6") !== -1
);

// Modern pattern
console.log(
  tags.includes("es6")
);
Feature indexOf() includes()
Return Value Index or -1 true or false
Readability Needs comparison Direct boolean check
NaN Support No Yes
Best For Finding position Checking existence

includes vs find

Method Returns Best For
includes() Boolean Checking if a value exists
find() Matching element or undefined Finding an object by condition
findIndex() Index or -1 Finding position by condition

Common includes Use Cases

  • Checking user roles and permissions.
  • Validating selected tags or categories.
  • Filtering arrays against allowed values.
  • Checking whether a string contains text.
  • Building search and autocomplete logic.
  • Preventing duplicate values before adding items.
  • Simple feature flag and option checks.

includes Best Practices

  • Use includes() when you only need a yes/no answer.
  • Prefer it over indexOf() !== -1 for readability.
  • Use find() when you need the actual matching item.
  • Remember object comparisons are by reference, not deep equality.
  • Pass fromIndex when searching part of an array intentionally.
  • Use lowercase normalization for case-insensitive string checks.
  • Keep arrays small or consider Set for frequent lookups.

Common includes Mistakes

  • Expecting includes() to compare object contents deeply.
  • Using includes() when the index position is needed.
  • Forgetting that string checks are case-sensitive by default.
  • Assuming includes() works like a regex search.
  • Checking large arrays repeatedly without caching or using a Set.
  • Confusing array includes() with string includes() behavior details.
  • Using includes() for complex conditions better suited to some().

Key Takeaways

  • includes() checks whether a value exists in an array or string.
  • It returns a boolean instead of an index.
  • It handles NaN better than indexOf().
  • Use it for simple membership and substring checks.
  • Choose find() or findIndex() when you need more than a boolean.

Pro Tip

If you only need to know whether a value exists, use array.includes(value) instead of array.indexOf(value) !== -1.