The Curious Case of 'typeof null' in JavaScript: A Historical Quirk

Sunny Sharma
Published in JavaScript
2 min read
24 May 2023

In the world of JavaScript, there are numerous peculiarities and idiosyncrasies that have left developers scratching their heads. One such oddity is the result of the expression typeof null, which returns "object" instead of "null." This behaviour has puzzled programmers for years, leading to debates and discussions. In this blog post, we delve into the historical context and shed light on why typeof null is, against expectations, evaluated as "object" in JavaScript.

Understanding typeof:

Before we dive into the specifics of typeof null, let's establish a foundation by exploring the typeof operator itself. In JavaScript, the typeof operator is used to determine the data type of a given value. It is a unary operator that takes an operand and returns a string representing the type of that operand.

The Null Object Reference:

In JavaScript, null is a special value that represents the absence of any object value. It is often used to indicate intentional non-existence or uninitialized variables. Logically, one might expect the typeof operator to identify null as its own distinct type. However, due to a peculiar historical reason, this is not the case.

A Historical Quirk:

During the early days of JavaScript, simplicity and performance were paramount concerns. To optimize the language's implementation, a tagging system was employed. Each value was tagged with a specific type identifier for efficient storage and retrieval.

In this system, the tag for an empty object was set to all-zeros, coincidentally the same representation as a null pointer in the C programming language. As a result, the tag assigned to null values in JavaScript was also interpreted as an object type.

Preserving Compatibility:

Although the incorrect identification of null as an object is widely recognized as a mistake, changing this behavior would have introduced significant compatibility issues. JavaScript has a vast ecosystem of existing codebases, libraries, and frameworks that rely on this quirk. Altering the behavior of typeof null to return a new type would have broken a significant amount of code.

Maintaining Backward Compatibility:

JavaScript has a strong commitment to backward compatibility, ensuring that code written in the past continues to function as intended in the future. Consequently, even though typeof null being evaluated as "object" is technically a bug, it has been maintained as a standard behavior to avoid breaking existing applications and causing widespread disruptions.

Alternative Approaches:

Given the known issues with typeof null, developers often resort to alternative methods to check for null values. Instead of relying solely on the typeof operator, more explicit null checks such as value === null or value == null are preferred. These approaches provide more reliable and accurate results when testing for null values.

Conclusion:

The unexpected result of typeof null as "object" in JavaScript can be attributed to a historical quirk. While it may seem like a mistake, it has become a standardized behavior due to the language's commitment to backward compatibility. Although this behavior has caused confusion and debates, developers have adapted by employing alternative methods to check for null values. Understanding the historical context behind typeof null sheds light on one of JavaScript's fascinating intricacies and serves as a reminder of the importance of maintaining compatibility in evolving programming languages.