
Why typeof null is "object" — The Bug That Became a Feature
The Mystery of typeof null If you've ever run console.log(typeof null) and stared at the screen in confusion when "object" popped up, welcome to the club. You haven't discovered a glitch in the matrix; you've stumbled upon one of JavaScript’s oldest historical artifacts. The Problem In JavaScript, we are taught that there are primitive types (string, number, boolean, undefined, symbol, bigint, and null) and objects. Logic suggests that typeof null should return "null" . Instead, we get this: console . log ( typeof null ); // "object" const myValue = null ; // ✅ The specific way if ( myValue === null ) { console . log ( " It's actually null! " ); } // 🧐 The "nullish" way if ( myValue == null ) { // This matches BOTH null and undefined // Useful if you want to catch both at once } 💬 Let's chat! The "Aha!" moment: When was the first time you encountered this quirk? Did it cause a bug in your code? The JS Quirks Club: What is your "favorite" JavaScript weirdness? Is it [] == ![] or maybe t
Continue reading on Dev.to Webdev
Opens in a new tab

