Last Updated on 11 June 2020
// The largest representable number console.log(Number.MAX_VALUE); // 1.7976931348623157e+308 // The smallest representable number console.log(Number.MIN_VALUE); // 5e-324
console.log(1.7976931348623157e+308); // 1.7976931348623157e+308 console.log(5e-324); // 5e-324
console.log(1.7976931348623157e+309); // Infinity console.log(5e-325); // 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
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
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
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?
// 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.min() function. It’s interesting to see the
Math.max() is smaller than
Math.min(). This is because the
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
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! email@example.com