DEV Community

Cover image for JavaScript is Crazy - Part 2
Dhairya Shah
Dhairya Shah

Posted on • Originally published at codewithsnowbit.hashnode.dev

JavaScript is Crazy - Part 2

Hello Folks 👋

What’s up friends, this is SnowBit here. I am a young passionate and self-taught developer and have an intention to become a successful developer.

Today, I am here again with a crazy thing in JavaScript.

So, let’s get started.

console.log(Math.max())
Enter fullscreen mode Exit fullscreen mode

As we all know the biggest positive number is ∞ (infinity). So the out of the above code snippet should be positive Infinity. For better understanding, you can see the attached graph

graph

console.log(Math.max())
// -Infinity
Enter fullscreen mode Exit fullscreen mode

But, as we know JavaScript is crazy 🤪; The output of this code is -Infinity. Don't believe it? check out the demo - https://jsfiddle.net/ce0hkoLs/

Now, here come Math.min() it is converse of Math.max

As we all know that lowest negative number is -∞(-infinity), but we know that’s not the case.

console.log(Math.min())
// Infinity
Enter fullscreen mode Exit fullscreen mode

We all know simple math, right? Now again we have a crazy thing with mathematics in JavaScript.

console.log(0.5 + 0.1 == 0.6)
// true
Enter fullscreen mode Exit fullscreen mode

Yes, 0.5 + 0.1 == 0.6 is true, isn’t it?

Now here comes some thing crazy,

console.log(0.1 + 0.2 == 0.3)
Enter fullscreen mode Exit fullscreen mode

As we know, 0.1 + 0.2 == 0.3 is true, but JavaScript don’t think that. Somehow, JavaScript thinks it is false, I actually don’t know the exact reason for that let me know in the comments if you know it.

There, the output will be,

console.log(0.1 + 0.2 == 0.3)
// false
Enter fullscreen mode Exit fullscreen mode

Try it - https://jsfiddle.net/feahj1s2/


Thank you for reading, have a nice day!
Your appreciation is my motivation 😊

Top comments (4)

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

It actually makes a lot of sense that the maximum of nothing is the lowest possible number.

Consider this example:

let a = [/* some numbers */]
let b = [/* some numbers */]
Math.max(Math.max(...a), Math.max(...b))
// Should be the same as
Math.max(...a, ...b)
Enter fullscreen mode Exit fullscreen mode

Consider now that a = [] and b = [2]

If Math.max(...[]), which is the same as Math.max() returned positive infinity, then the first example would be Math.max(Math.max(), Math.max(2)) which would return positive infinity, even though the second example would return 2 because Math.max(...[], ...[2]) is the same as just Math.max(2).


The second one is really just the age-old topic of floating point inaccuracy. It's interesting to explore why it happens with one set of numbers but not with another, but on its own, it's really just a well-known phenomenon.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

As for the second point, it is not really a JavaScript issue - rather it is an issue with the way floating point (decimal) numbers are stored internally. This website gives a fairly succinct explanation and examples of the issue in various languages.

Collapse
 
dhairyashah profile image
Dhairya Shah

haha, website is amazing specially url

Collapse
 
dhairyashah profile image
Dhairya Shah

Great explaination!