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.
What You'll Learn
💡 Real-World Analogy
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 manager: User | null 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.
Common Errors (and the fix)
- ❌ "'x' is possibly 'null'" — you accessed a possibly-missing value without checking. ✅ Use x?.prop or an if (x) guard before using it.
- ❌ Using || where you meant ?? — count || 10 wrongly replaces a real 0 . ✅ Use count ?? 10 so only null/undefined trigger the default.
- ❌ Testing null with typeof — typeof null is "object" , not "null" . ✅ Compare directly: value === null .
- ❌ Returning a value from a void function — TS rejects return 5 when the type is void . ✅ Either drop the return or change the return type.
Pro Tips
- 💡 Always enable strict (which includes strictNullChecks ) on new projects — it catches the most common runtime crash class.
- 💡 Prefer ?. + ?? over long && chains for reading deep, possibly-missing data.
- 💡 Pick one "nothing" value per codebase. Many teams default to undefined and only use null where an external API demands it.
- 💡 Use void for event handlers and callbacks whose return value is irrelevant — it documents intent and enables the return-ignoring rule.
📚 Keep learning
- TypeScript Handbook — void
- TypeScript Handbook — null and undefined
- TSConfig Reference — strictNullChecks
Frequently Asked Questions
Mini-Challenge: Safe Display Name
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.
🎉 Lesson Complete
- ✅ void is the return type for side-effect functions (runtime value is undefined )
- ✅ undefined = absent/not set; null = intentionally empty
- ✅ typeof null is "object" — test null with === null
- ✅ strictNullChecks removes the hidden null/undefined from every type
- ✅ ?. avoids crashes; ?? defaults only on null/undefined
- ✅ Next lesson: Structural Typing — why shape, not name, decides compatibility
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.
Continue this course
- Previous: any vs unknown vs never
- Next: Structural Typing