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.
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.
#1 Best Overall
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 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.
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.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.
Quick Recap
Best Value
Avoid these common mistakes
- Do not use
if (!value)for an undefined-only check. It also rejects0,false, and the empty string. - Do not use
value == undefinedwhen null must stay distinct. Loose equality intentionally matches bothnullandundefined. - 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 reportstypeof nullas"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.




