Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 23, 2019 03:03 am GMT

Math.min returns Infinity?

Preface

I was playing around with "Math.min/max" and the result of empty calls looked like it was backwards.

empty math min max results

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

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To