DEV Community

Cover image for "Unveiling JavaScript's Puzzling Quirk: The Object Identity of Null"
BIKI DAS
BIKI DAS

Posted on

"Unveiling JavaScript's Puzzling Quirk: The Object Identity of Null"

In the realm of frontend interviews, the question of typeof(Object) has left many developers scratching their heads. We've all experienced the puzzling result: null, a value representing explicit emptiness, is strangely classified as an Object. But why does this anomaly exist? Join me on an exploration of JavaScript's early design era, where Brendan Eich created the language in record time. In this article, we'll embark on a journey through the world of 32-bit values, type tags, and binary magic. Prepare to unravel the captivating story behind why null aligns with the Object type. Let's dive into the enigma and discover the untold secrets!

To understand the quirks of JavaScript, we must travel back to its inception. Brendan Eich's journey in creating JavaScript was a race against time, driven by the need for a versatile language to power the emerging web. In the blink of an eye, JavaScript was born, setting the stage for its intriguing behavior.

Computers comprehend only zeros and ones, and JavaScript values were no exception. Visualize a time when JavaScript values were confined to 32 bits, forming the building blocks of the language we know today. These 32 bits held the key to unraveling the mysteries of type determination.

Image description

The final 1-3 bits of those 32 bits were allocated as type tags.These type tags were the gatekeepers to understanding the nature of a particular JavaScript value. They held the power to distinguish between primitive types and objects.

Enter null, the sentinel value representing explicit emptiness. Null, at its core, is stored as a null pointer address consisting of all zeroes. This null pointer address aligns with the type tag assigned to objects in JavaScript, leading to the unexpected result when evaluating typeof null.

Image description

By delving into the type tag table, we uncover the hidden connection between null and the Object type. The type tag for objects is represented by all zeroes, which perfectly matches the null pointer address. As a result, the typeof null expression yields "object," puzzling developers worldwide.

I hope This article helped you to Uncover this mystic result of the typeof(Object), consider sharing the Article to let all of your fellow Programmers know and reveal the Mystery.

Top comments (2)

Collapse
 
tracygjg profile image
Tracy Gilmore • Edited

Hi Biki,

Is null the only falsy Object?

console.log(!!null); // false

console.log(typeof null); // object 

console.log(!!{}); // true

const isNull = subject => typeof subject === 'object' && !subject;

const isObject = subejct =>  typeof subject === 'object' && !!subject;
Enter fullscreen mode Exit fullscreen mode

Tracy

Collapse
 
j4k0xb profile image
j4k0xb

Image description