Difference Between null and undefined in JavaScript
Understand the difference between null and undefined in JavaScript, including typeof results, == vs === behaviour, examples and common interview questions.
Expected Interview Answer
undefined means a variable has been declared but not yet assigned a value, while null is an explicit, intentional assignment representing 'no value'. undefined is set by the JavaScript engine; null is set by the programmer.
typeof undefined returns 'undefined', but typeof null returns 'object' (a long-standing language quirk). They are loosely equal (null == undefined is true) because == does type coercion, but strictly unequal (null === undefined is false) because === checks type as well as value. Function parameters with no argument, missing object properties, and unassigned variables all yield undefined, whereas null is something you write deliberately to signal emptiness.
- Lets you distinguish 'never set' from 'deliberately empty'
- Makes intent explicit when clearing a value with null
- Helps write safer null-checks and default handling
- Clarifies API contracts about missing vs. absent data
- Avoids bugs from == vs === coercion surprises
AI Mentor Explanation
undefined is a batting slot on the scorecard that has not come to the crease yet — the engine simply left it blank because nobody filled it. null is a player the captain deliberately marked 'did not bat': a conscious human decision that the slot is intentionally empty, not merely unreached.
Step-by-Step Explanation
Step 1
Declare without assigning
Write let x; and read x — it is undefined because the engine assigned nothing.
Step 2
Assign null explicitly
Write let y = null; to intentionally mark the variable as holding no value.
Step 3
Check typeof
typeof undefined is 'undefined'; typeof null is 'object' — a known language bug to remember.
Step 4
Compare loosely
null == undefined is true because == coerces the two 'empty' values together.
Step 5
Compare strictly
null === undefined is false because === also compares types, which differ.
What Interviewer Expects
- Clear statement that undefined is engine-set and null is programmer-set
- Knowledge that typeof null returns 'object'
- Understanding of == vs === behaviour for the two
- Real cases that produce undefined (missing params, absent properties)
- When to deliberately use null
Common Mistakes
- Claiming typeof null returns 'null'
- Saying null === undefined is true
- Using them interchangeably without understanding intent
- Thinking undefined cannot be assigned manually
- Confusing 'not defined' (a ReferenceError) with undefined
Best Answer (HR Friendly)
“undefined means a value was never set — JavaScript filled the gap for you. null means you purposely said 'this is empty'. They look similar but one is accidental and the other is a deliberate choice by the programmer.”
Code Example
let a;
console.log(a); // undefined (declared, not assigned)
let b = null;
console.log(b); // null (explicitly empty)
console.log(typeof a); // 'undefined'
console.log(typeof b); // 'object' (historical quirk)
console.log(a == b); // true (loose: both are 'empty')
console.log(a === b); // false (strict: types differ)
function greet(name) {
console.log(name); // undefined if no argument passed
}
greet();
const user = { id: 1 };
console.log(user.email); // undefined (missing property)Follow-up Questions
- Why does typeof null return 'object'?
- What is the difference between == and === in JavaScript?
- How do you safely check if a variable is null or undefined?
- What does the nullish coalescing operator (??) do?
- What is a ReferenceError and how does it differ from undefined?
MCQ Practice
1. What does typeof null return?
typeof null returns 'object', a long-standing bug preserved for backward compatibility.
2. What is the result of null == undefined?
Loose equality treats null and undefined as equal, so null == undefined is true.
3. Which value does the JavaScript engine assign to an unassigned declared variable?
A declared but unassigned variable holds undefined until a value is given to it.
Flash Cards
What is undefined? — A variable declared but not assigned; the engine's default 'no value yet'.
What is null? — An intentional programmer-assigned value meaning 'no value / empty'.
typeof null? — 'object' — a historical JavaScript quirk, not an actual object.
null == undefined vs null === undefined? — == is true (coercion); === is false (types differ).