JavaScript: The Weird Numbers

The Weird Numbers
Last Updated on

This topic is talk about the JavaScript weird numbers that are strange sometimes. Before we starting that, do you know what is the smallest and largest representable value in JavaScript?

// The largest representable number
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308

// The smallest representable number
console.log(Number.MIN_VALUE); // 5e-324

Now we can try to print the largest and smallest number can be representable value in JavaScript.

console.log(1.7976931348623157e+308); // 1.7976931348623157e+308
console.log(5e-324); // 5e-324

Cool, it seems works right? But what if we print it larger and smaller than the representable value in JavaScript?

console.log(1.7976931348623157e+309); // Infinity
console.log(5e-325); // 0

Do you see that? The output seems so weird, it can’t represent the actual number now. JavaScript can’t represent the actual number if it over the max/min representable value. When it more than the representable value, it became INFINITY. If it less than representable value, it became 0.


We just talked about when the numbers is exceed the representable value, it will became Infinity right? What if we add, minus, multiple or divide it?

console.log(Infinity + 5); // Infinity
console.log(Infinity - 5); // Infinity
console.log(Infinity * 5); // Infinity
console.log(Infinity / 5); // Infinity

// What if we add, minus, multiple or divide with Infinity?
console.log(Infinity + Infinity); // Infinity
console.log(Infinity - Infinity); // NaN
console.log(Infinity * Infinity); // Infinity
console.log(Infinity / Infinity); // NaN

In all the case above, it they remain Infinity. You may surprise that when Infinity minus/divide from itself, it return will NaN.

Now, there another case that it may become Infinity. When numbers divided with 0, it became infinity.

console.log(10 + 0); // 10
console.log(10 - 0); // 10
console.log(10 * 0); // 0
console.log(10 / 0); // Infinity

But when 0 divided with 0. It don’t return Infinity, it returned NaN.

console.log(0 + 0); // 0
console.log(0 - 0); // 0
console.log(0 * 0); // 0
console.log(0 / 0); // NaN

Many times we think that -0 and +0 are same because when we compare both of it, it return true. But, they are doing totally different things in calculation. Let’s see how.

console.log(-0 === +0); // It returned true 
// But, they are doing totally different things in calculation

Next, the we tried to divide it with negative 0 to see how the number works in JavaScript. As you can see, when 0/-0 it returned NaN.

console.log(0 / -0); // NaN - it returned NaN because there is no -NaN
console.log(10 / -0); // -Infinity - But in here it became negative Infinity

JavaScript Numbers are 64-bit Floating Point, It only able to display accurate up to 15 digits. If the number are over 15 digits, the output might be difference.

console.log(999999999999999); // 999999999999999
console.log(9999999999999999); // 10000000000000000

However, the maximum number of decimals is 17. Like any others programming languages, the floating point arithmetic is not always accurate. Try out the code below.

console.log(0.1 + 0.2); // 0.30000000000000004

You may be surprised that in JavaScript, 0.1 + 0.2 === 0.3 is false. Looks weird right? If you don’t believe it, just give it a try. Now we going to try it in boolean.

0.1 + 0.2 === 0.3 // false

Unfortunately, it doesn’t work. To learn more about it, please read this for details. https://0.30000000000000004.com/


There is also weird case when comparing to the number you should aware. Let’s look at the code here.

1 < 2 < 3; // True
3 > 2 > 1; // False

Why it returned false when 3 > 2 > 1?
This is because because JavaScript executes the expressions from left to right.

// Step 1
3 > 2 > 1;

// Step 2
true > 1;

In the second step, when comparing true is greater than 1, it returned false. This is because true is consider equal to 1 too.

true == 1; // true
true + true === 2; // true
true - true === 0; // true

I also experiment with the Math.max() and Math.min() function. It’s interesting to see the Math.max() is smaller than Math.min(). This is because the Math.max() or Math.min() function returns the largest/lowest value number passed in the function. If you didn’t pass any parameter, it returned Infinity/-Infinity. If you pass the parameter isn’t a number it return NaN.

Math.max() > Math.min(); // false
Math.max() < Math.min(); // true

Math.max(); // -Infinity
Math.min(); // Infinity

Thanks for reading!I hope you enjoyed it, if you know something interesting too, please let me know! contact@jorcus.com