Learn JavaScript
❯ types.null_undefined

null vs undefined in JavaScript

$concept

undefined means no value assigned — uninitialized variables, missing arguments, absent properties, void function returns. null means deliberately empty — assigned by programmers to signal intentional absence. Both are falsy, both == each other, and they diverge everywhere else.

Convention: let the language produce undefined (don't assign it yourself), assign null for intentional emptiness, and default both away with ?? or default parameters. typeof null === "object" is a 1995 bug now permanent — check null with === null, never typeof.

$how it works
1

undefined: never given

Declared-but-unset variables, missing args, absent keys, void returns — the language's own I-don't-have-one.

2

null: deliberately empty

Assigned by people meaning nothing-here-on-purpose — the intentional counterpart to undefined's accidental.

3

== equates, === separates

null == undefined is true (the one sanctioned loose equality); === keeps them distinct — prefer === plus explicit handling.

4

typeof null lies

typeof null === "object" is a fossilized bug — use x === null for null checks, typeof only for undefined.

$example
let a;
let b = null;
console.log(a, b);
console.log(a == b, a === b);
Output
undefined null
true false
1a was never assigned (undefined); b was deliberately emptied (null) — different origins, both print plainly.
2Loose == treats them as equal (the sanctioned exception); strict === separates them.
3Default both with ??, check null with === null — never conflate the two in logic.
$use cases
Assigning null (never undefined) for intentional emptiness
Defaulting missing config with ?? covering both absences
Checking null explicitly instead of trusting typeof
Reading == null as the idiomatic covers-both check in legacy code

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.