Sub topic 1
null and undefined: two ways to say “no value”
Prerequisites
Assume you know basic TypeScript types, interfaces, union types, and tsconfig.json. You should also know that JavaScript has both null and undefined.
JavaScript uses both values to represent missing data, but they usually communicate different situations:
undefinedusually means that a value was not provided or has not been assigned.nullusually means that a value was deliberately set to “no object” or “no result.”
These are conventions, not rules enforced by JavaScript. Your project should choose a consistent convention at each boundary.
How JavaScript produces them
An uninitialized variable has the value undefined. Reading a missing object property also produces undefined. A function that does not return a value returns undefined.
let pending: string;
console.log(pending); // undefined
const settings = { theme: "dark" };
console.log(settings.language); // undefined
function logEvent(): void {
console.log("saved");
}
console.log(logEvent()); // undefinedYou create null explicitly:
const selectedUser = null;This often means that the program checked for a user but found none, or that the program intentionally cleared an earlier value.
JavaScript has some surprising behavior. The expression typeof null returns "object" because of an old language design error. Do not use typeof to distinguish null from objects. Use a direct comparison instead.
| Value | Common meaning | Example source |
|---|---|---|
| `undefined` | Missing, omitted, or not assigned | `user.name` when `name` is absent |
| `null` | Explicitly empty | `currentUser = null` |
TypeScript treats them as separate types
With strictNullChecks enabled, null and undefined are not assignable to ordinary types such as string:
let name: string = "Ada";
// name = null; // Error
// name = undefined; // Error
let maybeName: string | null = null;
let optionalName: string | undefined = undefined;The | symbol creates a union type. A union says that a value may have one of several types. Therefore, string | null means “a string or the explicit empty value,” while string | undefined means “a string or a missing value.”
You can include both when an API uses both representations:
function displayName(name: string | null | undefined): string {
if (name == null) {
return "Anonymous";
}
return name;
}The == null check is intentional here. In this specific form, it matches both null and undefined. After the early return, TypeScript knows that name is a string.
Choose a representation at boundaries
A database might return null for an empty column. A JavaScript caller might omit a property, producing undefined. A function can preserve that difference if it matters:
interface Profile {
nickname?: string;
avatarUrl: string | null;
}
function describe(profile: Profile): string {
const nickname = profile.nickname ?? "No nickname";
const avatar = profile.avatarUrl === null ? "No avatar" : "Has avatar";
return `${nickname}: ${avatar}`;
}
console.log(describe({ avatarUrl: null }));
console.log(describe({ nickname: "Ada", avatarUrl: "/ada.png" }));Here, nickname?: string means the property may be absent. avatarUrl: string | null means the property is present, but its value may intentionally be empty. That distinction helps callers understand what the data means.
Exercise
Exercise
What is the type of a variable that may contain a user name, null, or undefined, and which check handles both missing values?
Show answer
The type is string | null | undefined. The check value == null matches both null and undefined, allowing TypeScript to narrow the remaining value to string.