Arrays
includes()
Checks whether a value is in an array, or a piece of text is in a string.
Why it exists
"Is this in the list?" is one of the most common questions in code: is this filter selected, is this role allowed, is there a banned word in the comment, is this language supported. Without includes you'd write a loop with a flag. includes answers in one call and reads like a plain sentence.
Where it's used
- Access checks:
["admin", "editor"].includes(user.role). - Selected filters:
selected.includes("red"). - Searching text:
title.toLowerCase().includes(query). - Allowed values:
["en", "es"].includes(lang).
Syntax
array.includes(value)
string.includes(substring)Examples
["tea", "coffee"].includes("coffee"); // → true
"JavaScript".includes("Script"); // → trueconst allowed = ["admin", "editor"];
allowed.includes("editor"); // → true
allowed.includes("guest"); // → false[1, 2, 3].includes("2"); // → false — a string and a number differ
[NaN].includes(NaN); // → true===const title = "JavaScript course";
title.toLowerCase().includes("javascript"); // → trueResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
includes walks the array and compares each item with the value almost like ===: type matters, 1 and "1" are different. The only difference from === is that NaN is found ([NaN].includes(NaN) is true), though NaN === NaN is false.
As soon as a match is found, the search stops. If none is found, it's false.
For strings, includes looks for a substring and is case-sensitive: "Cat".includes("cat") is false.
Objects are compared by reference: includes({ id: 1 }) looks for the very same object, not a similar one.
Traps
How it differs from…
SetSet.haschecks membership almost instantly at any size;includesscans the array. For frequent checks against a big list, use a Set.some()andevery()some checks by a condition (say, an object's property);
includesby an exact value.