The quirk of null...Null

The quirk of null...Null

●1 ●1 ●7
calendar_today ago • schedule1 min read

In English, null means nothing, but in JavaScript, that nothing can become a headache. Let's overthink it and try to understand why and how.

As per MDN,

In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties.

Since null is one of the 7 primitive data types, it is not an object.

What about typeof null? It returns the string "object".

Well, though null is a primitive data type, typeof null is not. typeof null is just a string, the result of using typeof, which is a way to check the type of any value. And typeof null === "object" is nothing but a bug that was left unsolved intentionally, so old websites wouldn't break.

So what about typeof Number, typeof String etc. returning "function"?

That's a different thing, and it's called a constructor). By convention, a constructor starts with a capital letter, and it is completely different from the same name with a lowercase letter because JavaScript is case-sensitive. So, "number" is completely different from Number. One is the type label typeof returns for values like 5, and the other is a constructor function.

So why does typeof Null/Undefined return "undefined"?

It's easy to mix things up as a beginner, but there is no such thing in JavaScript as Null or Undefined with a capital N/U. They are undeclared names, and typeof doesn't throw an error on undeclared names.

typeof Null;        // "undefined"  (undeclared name; typeof doesn't throw)
Null;               // ReferenceError: Null is not defined

Separately, null & undefined have no constructor function or wrapper object. That's also why property access on them fails instead of autoboxing:

null.toString();       // TypeError
undefined.toString();  // TypeError

So what did we learn from this article? Literally nothing aka null.

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelski - Apr 23

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelski - Mar 19

5 Web Dev Pitfalls That Are Silently Killing Your Projects (With Real Fixes)

Dharanidharan - Mar 3

Sovereign Intelligence: The Complete 25,000 Word Blueprint (Download)

Pocket Portfolio - Apr 1
chevron_left
160 Points • 9 Badges
Bangladesh • armaneousuf.vercel.app
2Posts
1Comments
1Connections
The key to success is to distract the distractions

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!