October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Check if a Value Exists in an Array in TypeScript

Use Array.prototype.includes() for a true/false membership check in TypeScript. This guide covers when to use indexOf(), some(), or find(), how NaN and object identity behave, and how the lib setting affects includes().
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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 Programming Language - Software Engineer & Coder T-Shirt
  • 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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
interface 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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:

{
  "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 lib setting that includes ES2016 or later, so the compiler accepts includes().
  • 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, including NaN.
  • Use indexOf() only when you need the position, or when you must support a runtime without includes().
  • Use some() when you are testing a property or condition on objects.
  • Use find() when you need the object and must handle the undefined case.
  • Check your lib setting and your runtime targets before relying on includes().

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

“

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.