Making Sense Of \u201cSenseless\u201d JavaScript Features<\/h1>\nJuan Diego Rodr\u00edguez<\/address>\n 2023-12-28T10:00:00+00:00
\n 2023-12-29T10:05:55+00:00
\n <\/header>\n
Why does JavaScript have so many eccentricities!? Like, why does 0.2 + 0.1<\/code> equals 0.30000000000000004<\/code>? Or, why does "" == false<\/code> evaluate to true<\/code>?<\/p>\nThere are a lot of mind-boggling decisions in JavaScript that seem pointless; some are misunderstood, while others are direct missteps in the design. Regardless, it\u2019s worth knowing what<\/em> these strange things are and why<\/em> they are in the language. I\u2019ll share what I believe are some of the quirkiest things about JavaScript and make sense of them.<\/p>\n0.1 + 0.2<\/code> And The Floating Point Format<\/h2>\nMany of us have mocked JavaScript by writing 0.1 + 0.2<\/code> in the console and watching it resoundingly fail to get 0.3<\/code>, but rather a funny-looking 0.30000000000000004<\/code> value.<\/p>\nWhat many developers might not know is that the weird result is not really JavaScript\u2019s fault! JavaScript is merely adhering to the IEEE Standard for Floating-Point Arithmetic<\/strong><\/a> that nearly every other computer and programming language uses to represent numbers.<\/p>\nBut what exactly is the Floating-Point Arithmetic?<\/p>\n
Computers have to represent numbers in all sizes, from the distance between planets and even between atoms. On paper, it\u2019s easy to write a massive number or a minuscule quantity without worrying about the size it will take. Computers don\u2019t have that luxury since they have to save all kinds of numbers in binary and a small space in memory.<\/p>\n
Take an 8-bit integer, for example. In binary, it can hold integers ranging from 0<\/code> to 255<\/code>.<\/p>\n
\n 2023-12-29T10:05:55+00:00
\n <\/header>\n
0.2 + 0.1<\/code> equals 0.30000000000000004<\/code>? Or, why does "" == false<\/code> evaluate to true<\/code>?<\/p>\nThere are a lot of mind-boggling decisions in JavaScript that seem pointless; some are misunderstood, while others are direct missteps in the design. Regardless, it\u2019s worth knowing what<\/em> these strange things are and why<\/em> they are in the language. I\u2019ll share what I believe are some of the quirkiest things about JavaScript and make sense of them.<\/p>\n0.1 + 0.2<\/code> And The Floating Point Format<\/h2>\nMany of us have mocked JavaScript by writing 0.1 + 0.2<\/code> in the console and watching it resoundingly fail to get 0.3<\/code>, but rather a funny-looking 0.30000000000000004<\/code> value.<\/p>\nWhat many developers might not know is that the weird result is not really JavaScript\u2019s fault! JavaScript is merely adhering to the IEEE Standard for Floating-Point Arithmetic<\/strong><\/a> that nearly every other computer and programming language uses to represent numbers.<\/p>\nBut what exactly is the Floating-Point Arithmetic?<\/p>\n
Computers have to represent numbers in all sizes, from the distance between planets and even between atoms. On paper, it\u2019s easy to write a massive number or a minuscule quantity without worrying about the size it will take. Computers don\u2019t have that luxury since they have to save all kinds of numbers in binary and a small space in memory.<\/p>\n
Take an 8-bit integer, for example. In binary, it can hold integers ranging from 0<\/code> to 255<\/code>.<\/p>\n