What is includes()?
includes(value, fromIndex?) returns true if an array (or string) contains the given value and false otherwise; it is the readable, modern replacement for indexOf(value) !== -1.
How it compares values
Arrays use the SameValueZero algorithm: like ===, except that NaN equals NaN. So [NaN].includes(NaN) is true, while [NaN].indexOf(NaN) is -1.
Strings
"hello world".includes("world") checks for a substring and is case-sensitive. Lower-case both sides for case-insensitive search.
Objects are compared by reference
[{ id: 1 }].includes({ id: 1 }) is false, because the literal creates a different object. To check by content use some: list.some(item => item.id === 1).
Use cases
- Allow-lists:
["admin", "editor"].includes(user.role). - Replacing long
||chains:["png", "jpg", "webp"].includes(ext). - Simple search in strings.
Performance
includes is a linear scan (O(n)). For many lookups against a large list, build a Set once and use set.has(value), which is O(1).
fromIndex
Starts searching at that index; negative values count from the end.
Code example
1const roles = ["admin", "editor", "viewer"];2console.log(roles.includes("editor")); // true3console.log(roles.includes("Editor")); // false - case-sensitive45// Clean allow-list check6const canEdit = (role) => ["admin", "editor"].includes(role);7console.log(canEdit("viewer"), canEdit("admin")); // false true89// NaN: includes finds it, indexOf does not10console.log([1, NaN].includes(NaN), [1, NaN].indexOf(NaN)); // true -11112// Objects are compared by reference13const items = [{ id: 1 }, { id: 2 }];14console.log(items.includes({ id: 1 })); // false15console.log(items.some((i) => i.id === 1)); // true1617// Strings18const title = "Learning JavaScript in 2026";19console.log(title.includes("Script"), title.toLowerCase().includes("javascript")); // true true2021// Many lookups: use a Set22const blocked = new Set(["spam.com", "bad.io"]);23const emails = ["a@ok.com", "b@spam.com"];24console.log(emails.filter((e) => !blocked.has(e.split("@")[1]))); // ["a@ok.com"]
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.