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 desk2 min

How to Check If an Object Is Undefined in TypeScript

Check a value with value === undefined when only undefined counts as missing. Use value == null when both null and undefined should match.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use value === undefined to check whether a value is specifically undefined. TypeScript narrows the value after the check, so you can safely access its properties in the other branch. If both null and undefined mean “absent” in your code, use value == null instead.

Check for undefined and narrow the type

For an undefined-only check, compare with undefined using strict equality:

As an Amazon Associate I earn from qualifying purchases.

function getName(value: { name: string } | undefined) {
  if (value === undefined) {
    return "missing";
  }

  return value.name; // value is narrowed to { name: string }
}

The guard handles the missing case and lets TypeScript narrow value in the remaining branch. The TypeScript Handbook describes undefined and null as distinct types: Null and Undefined.

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

Choose the check that matches your intent

Check What it matches Use it when
value === undefined Only undefined null, if allowed, must remain a separate value.
value === null Only null You need to handle explicit null separately.
value == null Both null and undefined Either nullish value counts as absent. Loose equality is intentional here.
typeof value === "undefined" Only undefined You prefer an explicit JavaScript runtime type test.

For ordinary typed values, value === undefined is usually the clearest undefined-only test. The typeof form is also valid. TypeScript’s narrowing guide explains how equality and typeof checks help refine types.

Why truthiness is not an undefined check

if (value) tests whether a value is truthy; it does not test only for undefined. It also takes the false branch for valid values such as 0, false, and "".

function showCount(count: number | undefined) {
  if (count === undefined) {
    return "no count";
  }

  return `Count: ${count}`; // 0 is preserved
}

Use truthiness only when every falsy value is intended to follow the same path. For further detail, see the Handbook’s truthiness narrowing section.

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

Handle optional properties and parameters

An optional parameter or property can be undefined. With strictNullChecks enabled, its type reflects that possibility, so check before using it where necessary:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
type RecordWithItem = {
  item?: { label: string };
};

function consume(record: RecordWithItem) {
  const item = record.item;
  if (item === undefined) {
    return;
  }

  console.log(item.label);
}

Reading the property once and checking the local is useful when repeated property access might not be stable. TypeScript’s TypeScript 2.0 release notes describe how strict null checks make possibly nullish property access or function calls errors, and how optional properties and parameters include undefined.

Use optional chaining when you do not need a branch

Optional chaining avoids accessing through a nullish receiver and returns a short-circuited result:

const label = record.item?.label;

Use an explicit guard instead when your control flow needs to distinguish an undefined value from other states, such as returning a specific message or running different logic.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Enable strict null checks for useful compiler feedback

Enable strictNullChecks in tsconfig.json, or enable the broader strict setting, so TypeScript can flag unsafe uses of possibly undefined values. With strict null checks disabled, null and undefined can flow into other types, so a clean compile provides less protection. The Handbook explains this behavior in its Null and Undefined section.

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.

Avoid these common mistakes

  • Do not use if (!value) for an undefined-only check. It also rejects 0, false, and the empty string.
  • Do not use value == undefined when null must stay distinct. Loose equality intentionally matches both null and undefined.
  • Do not treat value! as a runtime check. The non-null assertion silences the compiler; it does not verify that a value exists.
  • Do not assume typeof value === "object" proves the value is usable. JavaScript reports typeof null as "object"; handle nullish cases explicitly where relevant.

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 *

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
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver 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.