Why undefined == null is true but null == 0 is false?

I'm Shubham (@shubhamsinghbundela), I'm a Software Engineer, a Full-stack developer, a tech enthusiast, and a technical writer here on @Hashnode. I have a strong zeal to share my acquired knowledge and I am also willing to learn from others.
undefined == null → true, but null == 0 → false (WHY?)
JavaScript equality is one of the most confusing topics for beginners.
Consider these three statements:
undefined == null // true
undefined === null // false
null == 0 // false
At first glance, all look like “empty values”… so why different results?
Let’s break this mystery step-by-step
Step 1 — Understand == vs ===
JavaScript has two equality operators:
| Operator | Name | Type Conversion |
== | Loose equality | Yes |
=== | Strict equality | No |
1 == "1" // true
1 === "1" // false
Step 2 — What is undefined?
undefined means:
A variable is declared but not assigned a value.
let a;
console.log(a); // undefined
It’s automatically assigned by JavaScript.
Step 3 — What is null?
null means:
An intentional empty value set by the developer.
let b = null;
So conceptually:
| Value | Meaning |
| undefined | Default empty (JS assigned) |
| null | Intentional empty |
Step 4 — Why undefined == null is true
This happens because of a special rule in JavaScript.
According to the ECMAScript specification:
nullandundefinedare equal to each other — and nothing else.
So:
undefined == null // true
null == undefined // true
JavaScript treats both as:
👉 “Absence of value”
But this rule applies only in loose equality (==).
Step 5 — Why undefined === null is false
Strict equality checks:
Value
Type
Let’s compare:
| Value | Type |
| undefined | undefined |
| null | object |
Yes — this is a known JavaScript bug/legacy behavior:
typeof null === "object" // true
So:
undefined === null // false
Different types → strict equality fails.
Step 6 — Why null == 0 is false
Now comes the tricky part.
Many developers think:
nullis empty → empty = 0 → sonull == 0should be true.
null Does NOT Convert to 0 in Equality
Why?
Because the equality rule says:
If one value is
null, it only equalsundefined— nothing else.
null == undefined // true
null == 0 // false
null == false // false
null == "" // false
No numeric conversion happens here.
Step 7 — But Wait… Comparisons Behave Differently
Here’s where JavaScript becomes weird
null >= 0 // true
null > 0 // false
null <= 0 // true
Why?
Because comparison operators convert null to number.
Number(null) // 0
So:
0 >= 0 // true
0 > 0 // false
Equality vs Comparison Difference
| Expression | Conversion | Result |
null == 0 | No | false |
null >= 0 | Yes | true |
null > 0 | Yes | false |
Best Practice
Always prefer strict equality:
if (value === null) { }
if (value === undefined) { }
Avoid:
value == null




