JavaScript's Quirky typeof null ===

JavaScript has one of the most famous quirks in programming: 👉 typeof null === "object" Yes… really. But why? Back in the early implementation of JavaScript (1995), values were stored in memory using type tags. Objects had a type tag of 0. Unfortunately, null was represented as a null pointer (0x00). When typeof checked the type tag, it saw 0 and returned "object". That bug shipped. And because fixing it would break massive amounts of existing code, it stayed. So what’s the real issue? Because of this: typeof null === "object" You cannot safely check for objects like this: typeof value !== "object" Why? Because null is not an object - but JavaScript says it is. The correct way to check: if (value !== null && typeof value === "object") { // safe object check } Or more explicitly: if (value && typeof value === "object") (when you’re okay excluding null and other falsy values) This is one of those historical decisions that shaped JavaScript forever. It’s not a feature. It’s a legacy artifact. And knowing it separates beginners from engineers who understand the language deeply. Akash Kadlag Hitesh Choudhary Jay Kadlag Chai Aur Code #JavaScript #WebDevelopment #Programming #LearningInPublic

  • text

To view or add a comment, sign in

Explore content categories