Null vs Undefined in JavaScript: Key Differences Explained with Examples
When learning JavaScript, one of the most confusing topics for beginners (and even experienced developers) is the difference between null and undefined. Both represent “nothingness,” yet they behave differently in code, data types, and comparisons.
In this blog, we’ll break down the difference between null and undefined in JavaScript with examples, a comparison table, and also look at a few common interview questions related to this topic.
What is Undefined in JavaScript?
In JavaScript, variables are undefined when no value has been assigned to them. Unlike many programming languages where variables need explicit initialization, JavaScript assigns undefined by default.
Example:
let x;
console.log(x); // Output: undefined
console.log(typeof x); // Output: undefined
Here, x has been declared but not assigned any value. Therefore:
- The value of
xisundefined. - The type of
xis alsoundefined.
If we assign a value later:
x = 10;
console.log(x); // Output: 10
console.log(typeof x); // Output: number
π Key Point:
If a variable has been declared but not initialized, it will always hold the value undefined.
What is Null in JavaScript?
null is an explicit assignment to indicate that a variable is empty or has no value. Unlike undefined, which JavaScript assigns automatically, null is set intentionally by the developer.
Example:
let y = null;
console.log(y); // Output: null
console.log(typeof y); // Output: object (this is a long-known JavaScript bug!)
π Key Point:
nullis a primitive data type in JavaScript.- The
typeof nullmistakenly returns"object", but it is still considered a primitive value.
Null vs Undefined Comparison
At first glance, both seem similar, but there are important differences when comparing them.
Example:
console.log(null == undefined); // true
console.log(null === undefined); // false
- With loose equality (
==),nullandundefinedare considered equal. - With strict equality (
===), they are not equal because their data types differ.
Null vs Undefined: Comparison Table
| Feature | null |
undefined |
|---|---|---|
| Definition | Represents an intentional absence of any object value | Default value of uninitialized variables |
| Assigned By | Explicitly set by the programmer | Automatically assigned by JavaScript |
| Data Type | Primitive (though typeof null returns "object") |
Primitive (its own type) |
Equality (==) |
null == undefined β true |
null == undefined β true |
Strict Equality (===) |
null === undefined β false |
null === undefined β false |
| Usage | Used when you want to intentionally clear or reset a variable | Used when a variable is declared but not assigned any value |
| typeof Result | "object" (JavaScript bug) |
"undefined" |
| Best Practice | Use when you want to represent “no value” explicitly | Use only when checking uninitialized variables |
Can You Assign Undefined Explicitly?
Yes! Although JavaScript assigns undefined by default, you can also explicitly assign it.
let z = undefined;
console.log(z); // Output: undefined
console.log(typeof z); // Output: undefined
π Pro Tip:
When possible, use null to represent “no value” instead of explicitly setting undefined. This makes your code more intentional and readable.
Common Interview Questions on Null vs Undefined
-
What is undefined in JavaScript?
A variable that is declared but not assigned a value isundefined. -
What will be the output of
undefined == nullandundefined === null?
undefined == nullβtrue
undefined === nullβfalse -
Is null an object in JavaScript?
No.nullis a primitive data type. Thetypeof null === "object"is a well-known historical bug in JavaScript. -
Can you explicitly assign undefined to a variable?
Yes, you can assignundefined, but it’s usually better to usenullwhen you want to reset or clear a value.
FAQs on Null vs Undefined in JavaScript
Q1. What is the main difference between null and undefined in JavaScript?
undefined is the default value for uninitialized variables, while null is an intentional assignment to indicate “no value.”
Q2. Is undefined a data type in JavaScript?
Yes. undefined itself is a type in JavaScript.
Q3. Why does typeof null return “object”?
This is a historical bug in JavaScript. Although null is a primitive type, typeof null incorrectly returns "object".
Q4. Which should I use: null or undefined?
Use null when you want to intentionally clear a value. Use undefined only when checking for uninitialized variables.
Q5. Is null equal to undefined?
null == undefined β true (loose equality)
null === undefined β false (strict equality)
Final Thoughts
While null and undefined may look similar, they serve different purposes in JavaScript:
undefinedβ default value of uninitialized variables.nullβ explicitly assigned to represent “no value.”
Understanding their differences is not only essential for writing clean code but also a frequent topic in JavaScript interviews.