null / undefined null and undefined
Last updated: 2026-10-22
null / undefined null and undefined
null means "intentionally empty"; undefined means "not assigned / missing". With strictNullChecks they are standalone types and must be handled before normal use.
| Group | Basic Types |
|---|---|
| Kind | Type |
| Strict mode | Required |
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
let nickname: string | null = null;
if (nickname !== null) {
nickname.toUpperCase(); // usable only after excluding null
}
let maybe = list.find(x => x.ok); // undefined means not found
⚙️ Parameters
| a?.b | Short-circuits to undefined when the left side is null/undefined, without throwing |
|---|---|
| a ?? b | Takes the right side when the left is null/undefined; 0 and "" are not skipped |
Returns:None — null/undefined are basic type annotations and produce no runtime code.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
Ada guest
0
(Runtime output after transpilation; with strictNullChecks on, calling missing.length directly is a compile error)
💡 Notes
⚠️ Note With strictNullChecks off, null/undefined assign to anything and null-pointer safety rests on luck. Always enable strict in new projects.
💡 Tip ?. and ?? are the golden pair: user?.address?.city ?? "unknown" reads and defaults in one line.
ℹ️ Info ?? falls through only for null/undefined, preserving 0, false and "" — the crucial difference from ||.
🏷️ Related Basic Types
💡 Related cases
More cases coming soon.
📚 Related tutorials
❓ FAQ
Q ?? vs ||?
A || treats every falsy value (0, "", false) as empty; ?? only null/undefined. Defaulting should almost always use ?? so 0 and "" are not clobbered.
Q How should null and undefined be used semantically?
A Convention: undefined = no value yet (missing argument/property); null = deliberately emptied. Be consistent within one API.
Q What type does optional chaining produce?
A T | undefined: if any link can be empty, the result carries undefined — follow with ?? or narrow.