Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →To check whether an array contains a value in TypeScript, call includes(). It returns true or false, and it is the right choice for a yes/no membership test on primitive values such as numbers and strings. It is a JavaScript built-in that TypeScript type-checks, not a TypeScript-specific operator, so the same call works in plain JavaScript.
The basic check with includes()
Declare the array with an explicit type, then call includes() with the value you are looking for:
As an Amazon Associate I earn from qualifying purchases.
const values: number[] = [1, 2, 3];
const target = 2;
const exists: boolean = values.includes(target); // true
TypeScript documents array types as T[] or Array<T>, and the element type you declare determines which values the compiler will accept as arguments to includes(). Passing a value of a different type, such as a string into a number[], is reported as a compile-time error.
Choose the method that matches the question
Membership is only one of several questions you can ask of an array. The method should match the result you need: a boolean, an index, or the element itself.
#1 Best Overall
| What you need | Method | Result when found | Result when not found |
|---|---|---|---|
| Is this exact value present? | values.includes(target) |
true |
false |
| Where is this exact value? | values.indexOf(target) |
First matching index | -1 |
| Does any element meet a condition? | users.some(user => user.id === targetId) |
true |
false |
| Which element meets a condition? | users.find(user => user.id === targetId) |
The matching element | undefined |
MDN Web Docs recommends includes() for checking whether a value exists, some() when any element must satisfy a test, and find() when you need the matching element itself.
Searching for objects and by property
Built-in membership methods compare object identity, not the fields inside the object. Two objects with identical contents are different values:
Rank #2
- TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
- TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
const a = { id: 1 };
const list = [a];
list.includes(a); // true, same reference
list.includes({ id: 1 }); // false, different object
When you want to match on a property such as an ID, use a predicate. The typed example below shows both the boolean check and the retrieval of the full object:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsinterface User {
id: number;
name: string;
}
const users: User[] = [
{ id: 1, name: "Ada" },
{ id: 2, name: "Linus" },
];
const targetId = 2;
const hasUser: boolean = users.some(user => user.id === targetId); // true
const match: User | undefined = users.find(user => user.id === targetId); // { id: 2, name: "Linus" }
Because find() can return undefined, TypeScript requires you to narrow the result before reading its properties.
Equality edge cases: NaN, zero, and empty slots
includes() uses the SameValueZero comparison. It behaves like strict equality for most values, with two differences: it treats NaN as equal to itself, and it treats positive and negative zero as equal. As a result:
[NaN].includes(NaN); // true
[NaN].indexOf(NaN); // -1
indexOf() uses strict equality, so it can never find NaN. If your array may contain NaN, use includes() or a predicate built on Number.isNaN().
The two methods also differ on sparse arrays. indexOf() skips empty slots, while includes() treats an empty slot as undefined. For example, [,1].includes(undefined) returns true, whereas [,1].indexOf(undefined) returns -1. Sparse arrays are uncommon in typed application code, but they can appear when an array is created with a large length and partly filled.
TypeScript configuration and runtime support
TypeScript’s lib compiler option controls which built-in JavaScript API declarations the type checker knows about. The TSConfig reference lists Array.prototype.find under ES2015 and Array.prototype.includes under ES2016. If your lib setting targets an older ECMAScript version, the compiler will report includes as missing:
Best Value
{
"compilerOptions": {
"lib": ["ES2016"]
}
}
The lib setting only affects type checking. It does not add the method to the runtime. Your project therefore needs both:
- A
libsetting that includes ES2016 or later, so the compiler acceptsincludes(). - A JavaScript runtime, such as a target browser or Node.js version, that implements
includes(), or a polyfill that supplies it.
If you cannot provide either, indexOf() is a compatible fallback when its strict-equality behavior is acceptable for your values. Remember that it returns an index, so compare the result with -1, not with a boolean.
Quick decision guide
- Use
includes()for a boolean check on primitive values, includingNaN. - Use
indexOf()only when you need the position, or when you must support a runtime withoutincludes(). - Use
some()when you are testing a property or condition on objects. - Use
find()when you need the object and must handle theundefinedcase. - Check your
libsetting and your runtime targets before relying onincludes().
The TypeScript 5.2 release notes describe special handling for methods such as find and some when they are called on a union of array types. That is an advanced case and is not needed for ordinary membership checks.
Quick Recap
“
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




