<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Anny He</title>
    <description>The latest articles on Forem by Anny He (@annyh).</description>
    <link>https://forem.com/annyh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F367658%2F5be065a0-7ba2-4ec4-b5ba-7e13bc083db6.jpeg</url>
      <title>Forem: Anny He</title>
      <link>https://forem.com/annyh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/annyh"/>
    <language>en</language>
    <item>
      <title>JavaScript Quirks: Numbers Edition</title>
      <dc:creator>Anny He</dc:creator>
      <pubDate>Thu, 16 Apr 2020 15:04:40 +0000</pubDate>
      <link>https://forem.com/heroku/javascript-quirks-numbers-edition-3ppa</link>
      <guid>https://forem.com/heroku/javascript-quirks-numbers-edition-3ppa</guid>
      <description>&lt;p&gt;As a developer working with JavaScript, perhaps you’ve run into “&lt;em&gt;huh?”&lt;/em&gt; moments from working with what some characterize as quirks of the language. On the face of it, each of the statements below presents an unexpected outcome. But if you dig deeper into how the datatypes and APIs are implemented, you’ll find there is always a reason behind these quirks.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0.1 + 0.2 !== 0.3; // output: true&lt;/code&gt;&lt;br&gt;
&lt;code&gt;parseInt('2/888') !== parseInt(2/888); // output: true&lt;/code&gt;&lt;br&gt;
&lt;code&gt;[100,1, 2, ].sort(); // output: [1, 100, 2]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, how many of these quirks belong to the JavaScript language, and how many are representative of Computing Science in general? In this post we’ll go deep into how JavaScript works with numbers so that you can avoid the most common mistakes you might make when working with Numbers in JS.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a JavaScript number?
&lt;/h3&gt;

&lt;p&gt;The Number data type is one of the 7 JavaScript data types. The other 6 being:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Undefined&lt;/li&gt;
&lt;li&gt;Null&lt;/li&gt;
&lt;li&gt;Symbol&lt;/li&gt;
&lt;li&gt;Object&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The object data type is the only &lt;em&gt;non-primitive&lt;/em&gt; data type. It includes dates, objects, and arrays, etc.&lt;/p&gt;

&lt;p&gt;The number data type is just as diverse. Numbers in JavaScript include integers, floats, binaries, hexadecimal… just to name a few. Examples of JavaScript numbers include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;123245&lt;/li&gt;
&lt;li&gt;1.234&lt;/li&gt;
&lt;li&gt;Infinity, -Infinity, 0&lt;/li&gt;
&lt;li&gt;0x31333131 // hexadecimal&lt;/li&gt;
&lt;li&gt;0232 // octal&lt;/li&gt;
&lt;li&gt;0b1111 // binary&lt;/li&gt;
&lt;li&gt;1.2e-1 // scientific notation&lt;/li&gt;
&lt;li&gt;NaN&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;NaN&lt;/strong&gt; is also a number! That sounds counterintuitive, yet it is true. To verify &lt;code&gt;NaN&lt;/code&gt; is a number, enter &lt;code&gt;typeof NaN&lt;/code&gt; in a terminal and press enter. The output is &lt;code&gt;"number"&lt;/code&gt;. &lt;code&gt;NaN&lt;/code&gt; is a popular concept in Computing Science and is prevalent in many programming languages. It simply means a unrepresentable number, yet it is represented like any other JavaScript number.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is not a JavaScript number?
&lt;/h3&gt;

&lt;p&gt;Not everything that looks like a number is a number. Any &lt;strong&gt;String&lt;/strong&gt; is not a number even when it looks like one. For example &lt;code&gt;‘1’&lt;/code&gt; is not a number because &lt;code&gt;typeof '1'&lt;/code&gt; is &lt;code&gt;"string"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;BigInt&lt;/strong&gt; is another &lt;strong&gt;Number&lt;/strong&gt; lookalike; it looks like an integer with ending n. For example &lt;code&gt;0n&lt;/code&gt;, &lt;code&gt;-4325325n&lt;/code&gt;, and &lt;code&gt;12345568574324546574534n&lt;/code&gt; are all BigInts. As the name suggests, BigInt only works for integers. BigInt &lt;em&gt;accurately&lt;/em&gt; represents integers, since not all integers can be represented using a JavaScript &lt;strong&gt;Number&lt;/strong&gt;. BigInt also looks, works, and is represented differently from numbers. This means that during BigInt to Number conversions, you risk potential data loss.&lt;/p&gt;

&lt;h2&gt;
  
  
  How are JavaScript numbers represented?
&lt;/h2&gt;

&lt;p&gt;Glad you asked! JavaScript numbers are represented using double precision floating point standard, specifically the &lt;a href="https://steve.hollasch.net/cgindex/coding/ieeefloat.html" rel="noopener noreferrer"&gt;IEEE 754 floating point standard&lt;/a&gt;. That’s a lot of terminology! What do &lt;em&gt;double precision&lt;/em&gt; and &lt;em&gt;floating point&lt;/em&gt; mean?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Double precision&lt;/strong&gt; means 64 bits to store each number. All else being equal, having more bits to store a number means being able to store more numbers accurately, so 64 bit storage means a bigger range than 32 bit storage. Some typed languages have the option to define a number as a &lt;strong&gt;float&lt;/strong&gt; (32 bits) or &lt;strong&gt;double&lt;/strong&gt; (64 bits). In JavaScript, all numbers, from floats to integers to special numbers, are represented in 64 bits.&lt;/p&gt;

&lt;p&gt;What about &lt;strong&gt;floating point representation&lt;/strong&gt;? Some programming languages such as Apex, C, and C++have the concept of &lt;strong&gt;int&lt;/strong&gt;, in addition to float and double. &lt;a href="https://www.dummies.com/programming/c/the-real-difference-between-integers-and-floating-point-values/" rel="noopener noreferrer"&gt;S&lt;/a&gt;&lt;a href="https://www.dummies.com/programming/c/the-real-difference-between-integers-and-floating-point-values/" rel="noopener noreferrer"&gt;tored differently&lt;/a&gt; from floating point, examples of &lt;strong&gt;ints&lt;/strong&gt; would include &lt;code&gt;1000&lt;/code&gt; or &lt;code&gt;2&lt;/code&gt; . However, in JavaScript, all numbers are stored using the 3 part floating point regardless of whether it has a decimal point or not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224155959%2F1-e1582588810329.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224155959%2F1-e1582588810329.png" alt="JavaScript number representation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s go over the three parts one by one&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Signed bit&lt;/strong&gt;: 0 for positive, 1 for negative&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exponent&lt;/strong&gt;: how large or small the number is. A multiplier for the fraction&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Significand/base/fraction&lt;/strong&gt;: used for more precise numbers. These bits represent increasing negative powers of 2&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Floating point representations
&lt;/h3&gt;

&lt;p&gt;Let’s see a few floating point representations for an overview of how it works.&lt;/p&gt;

&lt;p&gt;0’s representation is the most minimalist and the smallest binary possible. For negative numbers the signed bit will be 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224160118%2F2-e1582588889205.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224160118%2F2-e1582588889205.png" alt="0's floating point representation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For 1, notice the exponent bits go up to 011 1111 1111. In base 10 that binary is 1023. 1023 is important because if the exponent is smaller than that the number represented is between absolute one and zero. If the exponent is larger than that, the number represented is larger than 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224161510%2F3-e1582589719298.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224161510%2F3-e1582589719298.png" alt="1's floating point representation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For -1, notice the representation is identical to that of positive 1, except for the flipped signed bit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224161637%2F4-e1582589940530.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224161637%2F4-e1582589940530.png" alt="-1's floating point representation"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;For simplicity let’s go back to positive numbers. Take 1’s representation and increment the exponent by one to get 2. Notice the larger the exponent, the larger the number.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224162423%2F5-e1582590321286.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224162423%2F5-e1582590321286.png" alt="2's floating point representation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What about floats? 1.5 is a sum of 1 + 0.5, so its representation reflects that. Because significands are increasingly negative powers of 2, the first bit in the significand represents 1/2, and is flipped to 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224162559%2F6-e1582590371138.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224162559%2F6-e1582590371138.png" alt="1.5's floating point representation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1.75 is a sum of 1 + 0.5 + 0.25, so its representation reflects that. Note that the second bit in the significand represents 1/4, and is flipped to 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224162812%2F7-e1582590565479.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224162812%2F7-e1582590565479.png" alt="1.75's floating point representation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How does one represent &lt;code&gt;Infinity&lt;/code&gt; with a finite number of bits? Since &lt;code&gt;Infinity&lt;/code&gt; is a super large number, it makes sense to flip all the 1’s in the exponent to 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224163010%2F8-e1582590617441.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224163010%2F8-e1582590617441.png" alt="Infinity's floating point representation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is beyond Infinity? If you enter &lt;code&gt;Infinity + 1&lt;/code&gt; you get &lt;code&gt;NaN&lt;/code&gt;! Here is the &lt;em&gt;unrepresentable number&lt;/em&gt; represented just like any other number. Notice its representation is &lt;code&gt;Infinity + 1/2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224163106%2FNaN-e1582590725909.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224163106%2FNaN-e1582590725909.png" alt="NaN's floating point representation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How does one represent the largest safe integer? See below. Notice that all of the bits in the significand are flipped to 1, and the floating point is at the end of the 64 bit register.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224163305%2F9007-e1582590795176.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224163305%2F9007-e1582590795176.png" alt="The largest safe integer's floating point representation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What happens when we increment the &lt;code&gt;NUMBER.MAX_SAFE_INTEGER&lt;/code&gt; by 1? the floating point is floating off of the 64 bits, a clear indicator that the number is unreliably represented. &lt;code&gt;9007199254740992&lt;/code&gt; is unreliably represented because both itself and &lt;code&gt;9007199254740993&lt;/code&gt; map to the same representation. The further the floating point is off, the more bits are missing, thus the more likely the number is mis-represented. There are no errors: JavaScript silently fails to represent very large and small numbers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224163515%2F0992-e1582590923653.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224163515%2F0992-e1582590923653.png" alt="The largest safe integer's floating point representation incremented by 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When does the number representation fail?
&lt;/h3&gt;

&lt;p&gt;The representation will silently fail for &lt;em&gt;very small or very large numbers&lt;/em&gt;, because those numbers need more than 64 bits to be accurately represented. Their 64 bit representation is unreliable and potentially inaccurate.&lt;/p&gt;

&lt;p&gt;There is a &lt;strong&gt;safe range&lt;/strong&gt; to represent &lt;em&gt;integers:&lt;/em&gt; Integers from &lt;code&gt;-Math.pow(2, 53) + 1&lt;/code&gt; to &lt;code&gt;Math.pow(2, 53) - 1&lt;/code&gt; inclusive have 1:1 mapping between the number and its representation. Within this range, the integer is accurately represented. When outside of this range, consider using &lt;strong&gt;BigInt&lt;/strong&gt; to accurately store integers.&lt;/p&gt;

&lt;p&gt;To test out whether &lt;code&gt;yourNumber&lt;/code&gt; is within the safe range, use &lt;code&gt;Number.isSafeInteger(yourNumber)&lt;/code&gt; . The method outputs yes for integers smaller or equal to &lt;code&gt;Number.MAX_SAFE_INTEGER&lt;/code&gt;, and no for larger integers and floats.&lt;/p&gt;

&lt;p&gt;Unfortunately, there is no equivalent method to test the safeness of floats. Also you cannot use &lt;strong&gt;BigInt&lt;/strong&gt; to represent floats, since &lt;strong&gt;BigInt&lt;/strong&gt; only represents integers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quirky JavaScript number handling
&lt;/h2&gt;

&lt;p&gt;The floating point representation is a Computing Science concern, and the ensuing quirks are prevalent across programming languages such as Apex, C++, Java, and Python. In addition to floating point quirks, JavaScript also exhibits quirky behavior through its built-in methods. Let’s go over two popular gotchas*.*&lt;/p&gt;

&lt;h3&gt;
  
  
  Array.prototype.sort(optionalFunction)
&lt;/h3&gt;

&lt;p&gt;The out of the box &lt;code&gt;Array.prototype.sort(optionalFunction)&lt;/code&gt; is simple: it sorts the elements in increasing order and modifies the underlying array. It sorts a &lt;em&gt;string&lt;/em&gt; array, but does it sort a &lt;em&gt;numbers&lt;/em&gt; array?&lt;br&gt;
For example what is the output of &lt;code&gt;const arr = [100, 1, 2]; arr.sort();&lt;/code&gt; If it was sorted in ascending order we can expect &lt;code&gt;[1, 2, 100];&lt;/code&gt; However the result is different!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224164254%2Farr-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224164254%2Farr-1.png" alt="The output of const arr = [100, 1, 2]; arr.sort()"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result is unsorted and differs from the original! What is going on? According to &lt;a href="https://tc39.es/ecma262/#sec-array.prototype.sort" rel="noopener noreferrer"&gt;the official EMA spec on sort&lt;/a&gt;, since we omitted the comparator function, each element in the array is converted to a string then compared using the Unicode order. That’s how we got the &lt;code&gt;[1, 100, 2]&lt;/code&gt; result. Lesson learned! Always pass in a comparator function when dealing with number arrays. More details in the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel="noopener noreferrer"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  parseInt(stringOrNumber, radix)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;parseInt&lt;/strong&gt; is deceptively simple. Put in a string or number and get an integer out, right? A basic example of it working is &lt;code&gt;parseInt('2'); // outputs 2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Omitting the radix&lt;/strong&gt;&lt;br&gt;
Let’s start with the optional second parameter. What if you omit the radix (aka the base)? In other words, would these outputs be identical? &lt;code&gt;parseInt('0x32')&lt;/code&gt; vs &lt;code&gt;parseInt('0x32', 10)&lt;/code&gt;&lt;br&gt;
The only difference is that the second code snippet has 10 as the radix. If you think the default radix is base &lt;code&gt;10&lt;/code&gt;, then the results should be the same. But they differ! What is going on?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224164354%2FparseInt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224164354%2FparseInt.png" alt="Omitting the radix example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the first code snippet, &lt;strong&gt;parseInt&lt;/strong&gt; looks at the string and deduces that the underlying number is hexadecimal, since the string starts with &lt;code&gt;0x&lt;/code&gt;. Since &lt;code&gt;32&lt;/code&gt; in hexadecimal is &lt;code&gt;3 * 16 + 2&lt;/code&gt;, parseInt returns &lt;code&gt;50&lt;/code&gt;. For the second example, parseInt has the same string input, but &lt;code&gt;x&lt;/code&gt; is not in base 10, so everything from &lt;code&gt;x&lt;/code&gt; onwards is discarded. Hence the result is &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since the results differ, supply the radix to avoid surprises.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;String vs number&lt;/strong&gt;&lt;br&gt;
Moving onto another parseInt quirk: does parseInt treat the string input and number input equally? One might assume that since parseInt accepts both string and number, that it should treat them equally. So &lt;code&gt;parseInt('2/5556789', 10)&lt;/code&gt; should have the same output as &lt;code&gt;parseInt(2/5556789, 10)&lt;/code&gt;.&lt;br&gt;
Again the results differ. Let’s deconstruct what happened here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224164932%2Fparse-2-e1582591786128.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd259t2jj6zp7qm.cloudfront.net%2Fimages%2F20200224164932%2Fparse-2-e1582591786128.png" alt="String vs Number example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the first code snippet, &lt;strong&gt;parseInt&lt;/strong&gt; looks at the string &lt;code&gt;'2/5556789'&lt;/code&gt;. Because the &lt;code&gt;‘/’&lt;/code&gt; character is not in base 10, all characters from there onwards are discarded, and &lt;code&gt;2&lt;/code&gt; is returned. In the second code snippet, the first parameter is a number. The scientific notation for this number is &lt;code&gt;3.5992009054149796e-7&lt;/code&gt;, since large and small numbers have a tendency to be converted to scientific notation. parseInt correctly parses out 3 from that.&lt;/p&gt;

&lt;p&gt;Since the results differ from string to number, use parseInt with strings and avoid passing in numbers to it. To get integers from numbers, use &lt;code&gt;Math.round(number)&lt;/code&gt; for consistent and predictable results. In our example, &lt;code&gt;Math.round(2/5556789)&lt;/code&gt; correctly returns &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;There are plenty of quirks in JavaScript and plenty of quirks with numbers. This article scratched the surface of what can be quirky about JavaScript numbers, namely the &lt;strong&gt;parseInt&lt;/strong&gt; and &lt;strong&gt;Array.prototype.sort&lt;/strong&gt;. Use a comparator function for the &lt;strong&gt;sort&lt;/strong&gt; and always supply a radix with &lt;strong&gt;parseInt&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The inaccuracy of floating point representation is separate from JavaScript. The double precision representation is limited whenever the number requires more than 64 bits for accurate representation. Large and small numbers are prone to inaccuracies. Only numbers whose binary format is finite can be represented accurately with finite bits. Suffice to say, floating point &lt;strong&gt;approximates&lt;/strong&gt; numbers.&lt;/p&gt;

&lt;p&gt;Now you know everything about numbers! &lt;a href="https://lwc-quiz-app.herokuapp.com/" rel="noopener noreferrer"&gt;Take the quiz&lt;/a&gt; and see how well you do!&lt;br&gt;
Warning: the quiz might not be as easy as you think. Feel free to study the resources below before taking the test 🙂&lt;/p&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format" rel="noopener noreferrer"&gt;Wikipedia: Double precision IEEE 754 floating point format&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=MqHDDtVYJRI" rel="noopener noreferrer"&gt;JavaScript Numbers talk during JSConf EU 2013&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.itu.dk/~sestoft/bachelor/IEEE754_article.pdf" rel="noopener noreferrer"&gt;What every computer scientist should know about floating-point arithmetic&lt;/a&gt;&lt;br&gt;
&lt;a href="https://bartaz.github.io/ieee754-visualization/" rel="noopener noreferrer"&gt;IEEE 754 visualization&lt;/a&gt;&lt;br&gt;
&lt;a href="https://v8.dev/features/bigint" rel="noopener noreferrer"&gt;BigInt docs from V8 blog&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt" rel="noopener noreferrer"&gt;ParseInt MDN docs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://2ality.com/2013/01/parseint.html" rel="noopener noreferrer"&gt;ParseInt() doesn’t always correctly convert to integer&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/annyhe/lwc-quiz-app" rel="noopener noreferrer"&gt;Quiz source code&lt;/a&gt;&lt;br&gt;
&lt;a href="http://speakingjs.com/es5/ch11.html" rel="noopener noreferrer"&gt;On JavaScript numbers&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>data</category>
      <category>quirks</category>
    </item>
  </channel>
</rss>
