Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why Math.max(NaN, <any_number>) returns NaN?

Why comparing NaN to any number in Math.max() returns NaN??

For example Math.max(NaN,5,3,7) returns NaN.

Also Math.max(Infinity, NaN, 5,3) return NaN too.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

NaN is a falsy value.

But Math.max(false,4,5) does returns the largest i.e. 5.

I was getting some numbers from an API every second and compared the current fetched number with the previous one. But in some fetch it got NaN and after that the result was constantly NaN.

So what’s the reason for this?

>Solution :

Math.max tries to convert passed values to numbers. If it cannot do that, it returns NaN.

+true is 1

+false is 0

Math.max(true, 0) returns 1

Math.max(Infinity, 1) returns Infinity

Math.max(NaN,5,3,7) returns NaN

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading