Chapter 18: TypeScript Null & Undefined

Null and undefined in TypeScript very honestly and thoroughly — like we’re sitting together debugging real code and trying to understand why this topic confuses so many people (even experienced developers).

1. First — the core truth most tutorials skip

JavaScript has two different ways to say “no value here”:

Value Meaning Who/What usually creates it Typical origin
undefined “This thing has not been given a value yet” JavaScript engine (most of the time) Declared but not assigned, missing property, function without return
null “I intentionally set this to no value” You (the programmer) or some API obj.prop = null, JSON responses, database nulls, explicit reset

Very important sentence to remember forever:

In real-world TypeScript code written in 2025–2026 most bugs and most type errors involving null/undefined come from treating them the same or forgetting one of them exists.

2. How TypeScript sees them by default

TypeScript

By default → null and undefined are two separate types.

3. strictNullChecks = true (the setting that changes everything)

This is the single most important TypeScript setting for real applications.

JSON

When strictNullChecks: true:

TypeScript

Realistic modern type you see everywhere:

TypeScript

4. The most common real patterns in 2025–2026 code

Pattern 1: Optional chaining is your best friend

TypeScript

Pattern 2: Nullish coalescing ?? vs ||

TypeScript

Pattern 3: Definite assignment assertion ! (when you’re sure)

TypeScript

Pattern 4: Type narrowing (if / typeof / ===)

TypeScript
TypeScript

5. Quick comparison table (keep this in mind)

Check / Operator What it checks Use when you want… Safe with strictNullChecks?
value == null null or undefined Most common “missing value” check Yes
value === null only null Distinguish null from undefined Yes
value === undefined only undefined Check optional properties / missing args Yes
value ? … : … truthy / falsy “” → false, 0 → false (sometimes unwanted) Yes
value ?? … nullish (null or undefined) Keep “” and 0 Yes — very popular
value?.prop safe navigation Avoid crashes on null/undefined Yes
value! non-null assertion You are 100% sure it exists Yes — but use sparingly

6. Very common real bugs people still make (2026 edition)

TypeScript
TypeScript

7. Summary — modern mindset 2025–2026

  1. Use strictNullChecks: true (almost always)
  2. Prefer ?? over || for default values
  3. Love optional chaining ?.
  4. Use == null when you want to catch both null & undefined
  5. Narrow types with if, typeof, === null, === undefined
  6. Use ! only when you’re really sure (and it’s a small scope)
  7. When dealing with APIs → prefer null for intentional absence, undefined for “not present in object”
  8. In forms / state → lean towards “” / 0 / false instead of null/undefined when possible

Want to practice together?

Pick one of these and tell me — we can write & fix examples live:

  • Safe API response handling
  • Form default values with null/undefined
  • Deep nested object access without crashes
  • Distinguishing null vs undefined in real code
  • Writing a generic non-null assertion helper

Which one sounds most useful for you right now? 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *