Monday, June 25, 2018

Math.max in JavaScript returns the highest number from a set of numbers.

Consider:

alert(Math.max());
alert(Math.max(13));
alert(Math.max(42, 23));
alert(Math.max(13, 69, 27));
alert(Math.max(13, 42, 69, 86));
alert(Math.max(86, 23, 27, "foo", 42));

 
 

Alright, the alerts will say -Infinity, 13, 42, 69, 86, and NaN in that order. If we set all of the maxs to mins like this...

alert(Math.min());
alert(Math.min(13));
alert(Math.min(42, 23));
alert(Math.min(13, 69, 27));
alert(Math.min(13, 42, 69, 86));
alert(Math.min(86, 23, 27, "foo", 42));

 
 

...we get: Infinity, 13, 23, 13, 13, NaN

No comments:

Post a Comment