Typorithm · easy

null vs undefined

null-undefinedinterview-questionsnullundefinednullish-coalescing
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn null vs undefined

0%

JS Gotchas

undefined means a value was never set; null is an intentional empty. typeof undefined is "undefined", but typeof null is "object" — a famous bug. ?? supplies a default for both null and undefined, and ?. safely reads nested properties.

undefined is the default for variables and missing properties; null is an explicit "no value" you assign on purpose. typeof undefined is "undefined".

typeof null returns "object" — a well-known language bug preserved for backward compatibility, so never use typeof to detect null.

Nullish coalescing (x ?? "default") supplies a fallback only when x is null or undefined, and optional chaining (obj?.prop) reads nested values without throwing when an intermediate is nullish.

01

undefined = never set; null = intentional empty.

02

typeof null is "object" (a famous bug).

03

?? covers null AND undefined; ?. avoids throwing.

typeof undefined === "undefined" // true
typeof null === "object"         // true — a famous bug
const name = user.name ?? "default" // covers null AND undefined
const city = user.address?.city      // optional chaining, no throw

Read the implementation

Connect each part of the code to the concept before you type.

1. typeof undefined

undefined has its own type tag — typeof undefined is the string "undefined".

typeof undefined === "undefined" // true

2. typeof null bug

typeof null returns "object" — a long-standing language bug kept for backward compatibility.

typeof null === "object"         // true — a famous bug

3. Nullish coalescing

?? returns the right side only when the left is null or undefined, covering both at once.

user.name ?? "default"

4. Optional chaining

?. short-circuits to undefined if address is null or undefined instead of throwing.

user.address?.city

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

null vs undefined: Lesson & Practice · Typorithm | ApplyDjinn