null vs undefined vs undeclared
By FrontendPro Editorial Team Updated 9/26/2026
Answer
Undeclared means the name does not exist at all. undefined means it exists but has no value yet. null means it exists and was deliberately set to "no value".
let a; // declared, value is undefined
let b = null; // declared, value is null — you chose this
// c is undeclared
a; // undefined
b; // null
c; // ReferenceError: c is not defined
How to test for each
typeof c === 'undefined' // safe even when undeclared — no throw
a === undefined // distinguishes undefined from null
b == null // true for BOTH null and undefined
b === null // only null
typeof is the only operator that tolerates an undeclared name, which is why feature detection is written typeof fetch === 'undefined'.
Where each one comes from
undefined is what the engine hands you: an uninitialised variable, a missing function argument, a property that is not there, a function with no return. null only appears because someone wrote it — an API saying "this field is empty", document.querySelector finding nothing.
Two details that catch people out
typeof null is 'object', a bug from 1995 that can never be fixed. Check with === null.
And in JSON, null survives the round trip but undefined does not — JSON.stringify({ a: undefined }) gives '{}', so a field silently disappears.
A useful rule
Use x == null when you mean "empty either way" — it is the one place loose equality is worth it. Default parameters and ?? also treat only null and undefined as missing, unlike || which also swallows 0 and ''.
