JavaScript's 30-year-old 'object' bug

𝙩𝙮𝙥𝙚𝙤𝙛 𝙣𝙪𝙡𝙡 === '𝙤𝙗𝙟𝙚𝙘𝙩' 𝙞𝙨 𝙤𝙣𝙚 𝙤𝙛 𝙅𝙖𝙫𝙖𝙎𝙘𝙧𝙞𝙥𝙩'𝙨 𝙢𝙤𝙨𝙩 𝙛𝙖𝙢𝙤𝙪𝙨 𝙡𝙞𝙚𝙨. And it's not a bug that slipped through, it's a 30-year-old fossil that the web is too fragile to remove. 𝙃𝙚𝙧𝙚'𝙨 𝙩𝙝𝙚 𝙧𝙚𝙖𝙡 𝙨𝙩𝙤𝙧𝙮 👇 𝗜𝘁 𝘀𝘁𝗮𝗿𝘁𝘀 𝗶𝗻 𝟭𝟵𝟵𝟱. 𝘉𝘳𝘦𝘯𝘥𝘢𝘯 𝘌𝘪𝘤𝘩 built JavaScript in 10 days. The original engine stored every value as a 32-bit word, with the lower bits acting as a "𝘵𝘺𝘱𝘦 𝘵𝘢𝘨" to tell the engine what kind of value it was dealing with. The tag for objects? 000. 𝗡𝗼𝘄 𝗵𝗲𝗿𝗲'𝘀 𝘄𝗵𝗲𝗿𝗲 𝗶𝘁 𝗴𝗼𝗲𝘀 𝘄𝗿𝗼𝗻𝗴. 𝘯𝘶𝘭𝘭 was represented as a null pointer. literally all zeros in memory: 0x00000000. When typeof read the lower bits of null, it saw... 000. The same bits that meant "𝘰𝘣𝘫𝘦𝘤𝘵." So it returned '𝘰𝘣𝘫𝘦𝘤𝘵'. And nobody caught it before it shipped. 𝗧𝗵𝗲 𝗳𝗶𝘅 𝘄𝗮𝘀 𝗽𝗿𝗼𝗽𝗼𝘀𝗲𝗱. 𝗧𝗵𝗲𝗻 𝗿𝗲𝗷𝗲𝗰𝘁𝗲𝗱. In 2006, the ECMAScript committee tried to fix it. typeof null would finally return '𝘯𝘶𝘭𝘭'. Makes total sense. But by then, millions of websites had already shipped code that accidentally relied on 𝘵𝘺𝘱𝘦𝘰𝘧 𝘯𝘶𝘭𝘭 === '𝘰𝘣𝘫𝘦𝘤𝘵'. Changing it would've broken the web. So the bug stayed. 𝘉𝘳𝘦𝘯𝘥𝘢𝘯 𝘌𝘪𝘤𝘩 himself has called it a mistake. 𝗧𝗵𝗲 𝗱𝗲𝗲𝗽𝗲𝗿 𝘁𝗮𝗸𝗲𝗮𝘄𝗮𝘆: Every language has fossils like this. Decisions made under pressure, in 10 days, that the entire ecosystem quietly built on top of. The next time you write val !== null && typeof val === 'object', you're not just being defensive. You're working around a 30-year negotiation between perfection and backwards compatibility. That's not a JavaScript quirk. 𝗧𝗵𝗮𝘁'𝘀 𝗲𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝗶𝗻𝗴. 💬 𝘞𝘩𝘢𝘵'𝘴 𝘺𝘰𝘶𝘳 𝘧𝘢𝘷𝘰𝘶𝘳𝘪𝘵𝘦 𝘑𝘚 𝘲𝘶𝘪𝘳𝘬? 𝘋𝘳𝘰𝘱 𝘪𝘵 𝘣𝘦𝘭𝘰𝘸 #JavaScript #WebDevelopment #Programming #Frontend #TIL #SoftwareEngineering #React #TypeScript

  • text

This story never gets old. It’s wild how a 10-day deadline shaped a 30-year “feature” we still code around today.

pretty sure even an array can be seen as object in javascript. that's why you need to use the Array.isArray() method

See more comments

To view or add a comment, sign in

Explore content categories