JavaScript Null vs Undefined
Difference Between null & undefined
| Value | Meaning |
|---|---|
| null | A value has been deliberately set to "no value". |
| undefined | A value is expected. No value has been assigned yet. |
Examples
let x = null;
let a = x; // a = null
Try it Yourself »
let x;
let a = x; // a = undefined
Try it Yourself »
Null and undefined are different values.
| Null | Undefined |
|---|---|
| Intentional absence of a value | No value assigned |
Type is null |
Type is undefined |
typeof is "object" |
typeof is "undefined" |
Comparing Null and Undefined
Loose equality treats null and undefined as equal.
Strict equality does not.
Comparing Type
undefined and null are different values.
| Expression | Result |
|---|---|
typeof undefined |
"undefined" |
typeof null |
"object" |
Nullish Coalescing
The nullish coalescing operator (??) treats both null and undefined as missing values.
Examples
let person = undefined;
let name = (person ?? "unknown");
Try it Yourself »
let person = null;
let name = (person ?? "unknown");
Try it Yourself »