Why Math.min() > Math.max() is true in JavaScript

  sonic0002        2022-05-02 05:27:07       7,060        2    

JavaScript is a language which gives lots of surprises who are learning or understanding the first time. It has lots of features which look anti-intuitive at the first glance but they are understandable if checking deeper. In this post, one "feature" will be introduced.

Math.min() > Math.max() is TRUE

At the first glance, lot os people would think how it can be possible. However, the result is true.

console.log(Math.min() > Math.max()); // true
console.log(Math.min());  // Infinity
console.log(Math.max());  // -Infinity

The reason behind this is that Math.min() will return positive infinity if no parameter is passed while Math.max() will return negative infinity if no parameter is passed.

According to MDN, Math.min() return value will be.

The smallest of the given numbers. If any one or more of the parameters cannot be converted into a number, NaN is returned. The result is Infinity if no parameters are provided.

Math.max() return value will be:

The largest of the given numbers. If any one or more of the parameters cannot be converted into a number, NaN is returned. The result is -Infinity if no parameters are provided.

If looking into the details of the specification, can see why(step 3) it would return negative infinity if no parameter is passed.

This is just to explain why the fact is shown above. It doesn't judge why such decision. is made like this for the language. 

Really recommend everyone try to learn this language if time permits to gain more fun.

JAVASCRIPT  COMPARISON  MATH.MIN()  MATH.MAX() 

       

  RELATED


  2 COMMENTS


Anonymous [Reply]@ 2022-05-24 02:29:41

So many typos and grammatical erorrs. You should proof read your article.

Anonymous [Reply]@ 2022-05-24 02:30:21

Errors*



  RANDOM FUN

Chrome, itadakimasu