void, null & undefined
void is the return type of a function that yields no useful value, undefined means a value is absent or not yet set, and null means a value is intentionally empty — TypeScript tracks all three so missing data can't surprise you.
Learn void, null & undefined in our free TypeScript course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free TypeScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Think of a parcel locker slot. undefined is a slot that was never filled — nobody put anything in. null is a slot that staff deliberately emptied and labelled "reserved, intentionally empty". void is the receipt you get for an action like "lock the door": the action happened, but the receipt itself carries no useful contents to read.
1. void : Functions That Return Nothing
A function annotated : void is called for its side effect — logging, saving, updating the DOM — not for a value. At runtime it still returns undefined , but void is the type that tells callers "don't use my return value." There's a subtle but important rule: a void return type in a callback means "I'll ignore whatever you return," so callbacks that do return something are still accepted.
2. null vs undefined
These two represent "nothing" but carry different intent. undefined is what you get from an uninitialised variable, a missing object property, or a missing argument — "not there." null is a value you assign on purpose to mean "deliberately empty." They're distinct ( null === undefined is false ), and an optional property age? adds undefined to a type, whereas opts into null .
3. strictNullChecks, ?. and ??
With strictNullChecks on (part of strict ), a plain string can't secretly be null — you must opt in and then prove the value is present before using it. Two operators make that painless at runtime: optional chaining ?. short-circuits to undefined instead of crashing, and nullish coalescing ?? supplies a fallback for only null or undefined (unlike || , which also replaces 0 and "" ).
🎯 Your Turn
Supply a default greeting only when the name is missing. Fill in the blank marked ___ with the right operator, then run it.
No blanks this time — just a brief and a starting outline. Build the name builder yourself, run it, and check your output against the example in the comments.
Practice quiz
The 'void' type is used mainly as:
- A value you store in a variable
- An array element type
- A function's return type for side-effects
- A replacement for null
Answer: A function's return type for side-effects. void marks a function that returns nothing useful — callers should ignore its return value.
At runtime, a function typed to return void actually returns:
- undefined
- null
- 0
- An empty string
Answer: undefined. Such a function still returns undefined at runtime; void is just the type signalling 'ignore it'.
What does 'undefined' typically mean?
- Intentionally emptied
- A syntax error
- An empty array
- A value is absent or not yet set
Answer: A value is absent or not yet set. undefined is what you get from an uninitialised variable, a missing property, or a missing argument.
What does 'null' typically convey?
- A value that was never declared
- A value deliberately set to empty
- A function with no body
- A frozen object
Answer: A value deliberately set to empty. null means intentionally empty — you assign it on purpose to mean 'no value'.
What is the result of null === undefined?
- false
- true
- An error
- undefined
Answer: false. They are distinct values, so strict equality null === undefined is false (though null == undefined is true).
typeof null evaluates to:
- 'null'
- 'undefined'
- 'object'
- 'void'
Answer: 'object'. typeof null is the string 'object' — a long-standing JavaScript quirk; test null with === null.
An optional property written as age?: number means its type is:
- number only
- number | undefined
- number | null
- never
Answer: number | undefined. The optional ? adds undefined to the type and lets you omit the property entirely.
Turning on strictNullChecks means a plain 'string' type:
- Secretly includes null and undefined
- Becomes any
- Cannot be used
- Genuinely excludes null and undefined
Answer: Genuinely excludes null and undefined. strictNullChecks removes the hidden null/undefined membership; you must opt in with | null or | undefined.
The optional chaining operator ?. on a missing value yields:
- null
- undefined
- An exception
- 0
Answer: undefined. ?. short-circuits and yields undefined instead of crashing when something is null or undefined.
Unlike ||, the nullish coalescing operator ?? falls back ONLY on:
- 0 and empty string too
- false only
- null or undefined
- Any truthy value
Answer: null or undefined. ?? supplies a default only for null or undefined, so 0, '', and false are kept as real values.