Skip to main content

Command Palette

Search for a command to run...

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

Published
3 min read
Why undefined == null is true but null == 0 is false?
S

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:

OperatorNameType Conversion
==Loose equalityYes
===Strict equalityNo
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:

ValueMeaning
undefinedDefault empty (JS assigned)
nullIntentional empty

Step 4 — Why undefined == null is true

This happens because of a special rule in JavaScript.

According to the ECMAScript specification:

null and undefined are 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:

  1. Value

  2. Type

Let’s compare:

ValueType
undefinedundefined
nullobject

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:

null is empty → empty = 0 → so null == 0 should be true.

null Does NOT Convert to 0 in Equality

Why?

Because the equality rule says:

If one value is null, it only equals undefined — 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

ExpressionConversionResult
null == 0Nofalse
null >= 0Yestrue
null > 0Yesfalse

Best Practice

Always prefer strict equality:

if (value === null) { }
if (value === undefined) { }

Avoid:

value == null

More from this blog

Shubham Tech. Blog's

55 posts

Problem Solver | Currently Working As a Full Stack Developer, Community Leader At @Dev_Matrix | Previously Contributor at @RealDevSquad, @TeamShiksha