An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
Math.min returns Infinity?
Preface
I was playing around with "Math.min/max" and the result of empty calls looked like it was backwards.
Question
Shouldn't Math.min
return -Infinity
while Math.max
, Infinity
?
MDN Documentations
MDN documentation on Math.max shows that
If no arguments are given, the result is -Infinity.
And Math.min documentation shows
If no arguments are given, the result is Infinity.
But why?
After some thinking, it made sense.
Suppose that you are passing one value to Math.min(3)
. The minimum should 3
as it's the sole value pass to the function. 3
should be lower than whatever the minimum JavaScript has to compare.
Any value other than Infinity itself (Infinity === Infinity
is true
) should be the minimum, and as 3
is smaller than Infinity
, 3 is returned by Math.min
.
Same thing for Math.max
. If you call Math.max(3)
, 3
is bigger than -Infinity
thus, 3
is returned.
But...
I am not exactly sure if my thought process is correct or not but at least it helps to understand what default values are returned when no argument is passed to Math.min/max
functions.
Would anyone let me know if I understood the reason behind the return values?
Original Link: https://dev.to/dance2die/math-min-returns-infinity-1bi6
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To