In JavaScript, null is an explicit value meaning "no object here", and undefined is the value the engine uses when something has not been given a value. They are different types: typeof undefined is "undefined", while typeof null is "object" because of a legacy bug. Use === to tell them apart and ?? or ?. to handle both.
This guide focuses on the practical differences you hit in real code: function parameters, object properties, type coercion and TypeScript.
| Property | null | undefined |
|---|---|---|
| Meaning | Intentional absence of a value | Value not assigned |
typeof | "object" (legacy quirk) | "undefined" |
| Set by | The developer, and some web APIs | The JavaScript engine |
Number(x) | 0 | NaN |
String(x) | "null" | "undefined" |
| Falsy | Yes | Yes |
| Strict equality with each other | false | false |
| Loose equality with each other | true | true |
Default parameter values only replace undefined. A null argument is kept as it is:
If null should also fall back to a default, handle it explicitly:
A property set to undefined still exists, while a deleted or never-defined property does not. Reading either returns undefined, so use in or Object.hasOwn() (ES2022) to tell them apart:
Accessing a property on null or undefined throws a TypeError. Optional chaining avoids the error:
null becomes 0 in numeric contexts, while undefined becomes NaN, which can hide bugs:
Relational operators (>=) convert null to 0, while == does not, which is why null >= 0 is true yet null == 0 is false. Avoid comparing null with numbers.
Do not use !value as a null check: it is also true for 0, '', false and NaN.
With strict enabled, TypeScript treats them as distinct types, so the compiler forces you to deal with them:
A common convention is undefined for optional or missing fields and null for a value that was fetched and is explicitly empty, but teams should pick one rule and keep to it.
|| for defaults. count || 10 replaces a valid 0; write count ?? 10.typeof value === 'object' to find objects. It is also true for null, so add value !== null.undefined as the same. Use Object.hasOwn() when the difference matters, for example when merging configuration.undefined from one branch and null from another in the same function. Callers then need two checks; pick one.null becoming 0 in arithmetic. Convert explicitly with Number(value ?? 0).undefined means no value has been assigned. null means a value was assigned and it is "nothing".
The loose equality algorithm defines null and undefined as equal to each other and to nothing else. Strict equality also compares types, and the types differ.
No. JSON.stringify omits properties with undefined values, and JSON.parse can only produce null.
Return null for "looked up, nothing found" and keep the choice consistent across your API. Returning nothing yields undefined automatically.
undefined is the engine's "not set", and null is the developer's "empty". Remember that default parameters only trigger on undefined, that typeof null is "object", and that ?? and ?. are the safest ways to handle both.