undefined is the default for uninitialized variables, missing function args, missing object properties
null must be explicitly assigned
null == undefined is true (loose), null === undefined is false (strict)
null vs undefined under == and ===. Switch to === or compare null with 0 under Tweak.
Both are nullish: They mean "no value": undefined by default, null on purpose.
null == undefined is true: A special rule: they are loosely equal to each other and nothing else.
null === undefined is false: Different types.
typeof differs: typeof undefined is "undefined"; typeof null is "object".
Code examples
undefined Scenarios
// Declared but not assigned
let a;
console.log(a); // undefined
// Missing function parameter
function greet(name) {
console.log(name);
}
greet(); // undefined
// Non-existent object property
const obj = { x: 1 };
console.log(obj.y); // undefined
// Function with no return
function doNothing() {}
console.log(doNothing()); // undefined
console.log(typeof undefined); // "undefined"
JavaScript assigns undefined automatically in these cases.
null Usage
// Intentional empty value
let user = null; // user will be assigned later
console.log(user); // null
console.log(typeof null); // "object" (known JS bug)
// Clearing a reference
let data = { key: "value" };
data = null; // explicit cleanup
// Checking for both
function isEmpty(val) {
return val == null; // catches both null and undefined
}
console.log(isEmpty(null)); // true
console.log(isEmpty(undefined)); // true
console.log(isEmpty(0)); // false
console.log(isEmpty("")); // false
null is used intentionally. The == null check is a common shorthand for checking both null and undefined.
Key points
typeof null === "object" is a known bug from the first JavaScript implementation
Use val == null to check for both null and undefined at once
undefined is a type itself, null is an object (technically)
JSON.stringify ignores undefined properties but keeps null
Common follow-up questions
Why does typeof null return "object"?
How does nullish coalescing (??) handle null vs undefined?