DEV Community

Cover image for The JavaScript Riddle: Cracking the Code of typeof null
Md. Asif Rahman
Md. Asif Rahman

Posted on

The JavaScript Riddle: Cracking the Code of typeof null

Introduction:

JavaScript, a language known for its versatility, has a little surprise that can puzzle developers. It has to do with the typeof operator when used with the value null. Oddly, instead of saying null, it says object. In this article, we'll dig into why this happens, where it came from, and how developers can deal with this unexpected twist.

Historical Context:

In the early days of JavaScript, the language was crafted with a simple set of types: "undefined," "boolean," "number," "string," "object," and "function." When the null value was introduced to represent the intentional absence of any object or primitive value, a small design mistake (or perhaps a necessity at that time) occurred. Consequently, when using typeof with null, instead of identifying it as null, it mistakenly reports "object" due to this historical design hiccup.

Despite recognizing this as a mistake, correcting it poses challenges. JavaScript has a commitment to maintaining backward compatibility, and altering the behavior of typeof null might break existing code that relies on this historical quirk. So, developers are encouraged to be aware of this behavior and use other means, such as strict equality (===), to accurately check for null values. This reflects the evolution of the language and the complexities that can arise from decisions made in its early development stages.

According to the ECMAScript specification (the rules that govern JavaScript), null is officially defined as the special value that represents the deliberate absence of any object (you can find this in ECMA-262, section 11.4.3).
ECMA-262 - section 11.4.3

Let's inspect what the NULL actually is:

Even though the result of typeof null might seem odd, it's crucial to know that null isn't really an object like other things in JavaScript. It's a basic value, a special one that shows there's no specific object available. So, despite what typeof might say, null is more like a simple placeholder for the absence of an object.

Have a look at the below code snippets:

console.log(typeof null);  // Outputs: "object"
console.log(null instanceof Object);  // Outputs: false
Enter fullscreen mode Exit fullscreen mode

The instanceof operator, commonly used for determining an object's type, reveals that null is not an instance of the Object constructor.

Conclusion:

Let's sum it up in simpler terms. The odd behavior we observed is like a peculiar artifact from JavaScript's past. Don't let it throw you off! Think of it as a little surprise left by the language's creators.

So, how do you handle it? Well, just use a trick you probably already know: strict equality. Instead of relying on typeof, stick with === when you want to check if something is null. It's like saying, "Hey, are you exactly null? No funny business!"

Top comments (0)