Two small operators that remove a lot of defensive code.
Direct answer
Optional chaining (?.) safely reads a property or calls a function when a value may be null or undefined. Nullish coalescing (??) supplies a default only when the value is null or undefined. Together they handle missing data cleanly.
Examples
const city = user?.address?.city;
const length = items?.length ?? 0;
user?.sendMessage?.("Hello");
const count = 0;
console.log(count || 10); // 10, because 0 is falsy
console.log(count ?? 10); // 0, because 0 is not null
When to use which
| Situation | Use |
|---|---|
| Might be missing | ?. |
| Default only for null or undefined | ?? |
| Default for any falsy value | the or operator |
Where Teyro fits
Teyro's TypeScript for Beginners course turns this into short lessons you can play in a few minutes a day. It has 20 lessons, and you can start free.
The bottom line
?. to read safely, ?? to default safely.



