• Why math.max () is Less Than math.min () in JavaScript
  • Dr. Derek Austin 🥳
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: zhanght9527

Why is math.max () smaller than math.min () in JavaScript?

Math.max() < Math.min() === true

Is it surprising? This is why the maxima function in JavaScript is smaller than the minima function when no arguments are passed.

Do you know why math.max () returns a smaller value in JavaScript than math.min () without passing an argument?

console.log(Math.max() < Math.min()) // true
Copy the code

Why is that? Let’s examine what this function returns:

console.log(Math.max()) // -Infinity
Copy the code

Weird — Infinity is actually the largest Number in JavaScript, along with number. MAX_VALUE and number. MAX_SAFE_INTEGER.

So what does math.min () return with no arguments?

console.log(Math.min()) // Infinity
Copy the code

Once again, we expect the opposite — in JavaScript -infinity should be the smallest Number, along with number.min_value.

So why do math.min () and math.max () actually have opposite values than we expect?

The answer lies in MDN Docs:

“-infinity is the initial comparison term because almost all other values are larger than it, which is why -infinity is returned when no argument is given.

If at least one argument prevents it from being converted to a number, the result will be NaN. “– math.max () document in MDN

Of course! Math.max() is just a parameter implementation based on a for loop (see Chrome V8’s implementation).

Therefore, math.max () will start the search with -infinity because any other number is greater than -infinity.

Similarly, math.min () searches from Infinity:

“If no arguments are passed, Infinity is returned.

If at least one parameter cannot be converted to a number, NaN is returned.” — MDN Docs for math.min ()

The ECMAScript specification also states for math.max () and math.min () that +0 is considered greater than -0 by these functions:

console.log(Math.max(+0.0)) / / 0
console.log(Math.min(+0.0)) / / - 0
console.log(+0 > 0) // false
console.log(+0 > 0) // false
console.log(+0= = =0) // true
console.log(+0= =0) // true
console.log(Object.is(+0.0)) // false
Copy the code

This behavior differs from the > greater and \< less operators, which assume that -0 minus zero equals +0 plus zero.

Technically, the -0 minus zero operator is equal to 0 plus zero according to == and === equality, not according to Object.is().

Thus, in a sense, math.max () and math.min () are more elegant than simply implementing -0 minus zero (see lines 96-99 in the V8 code).

Did you like this article? Then you’ll like my article on the fastest way to find Max and min values in JavaScript arrays — I’ve shown you a way to use math.max () and math.min () rather than using… Extension operators are faster: The fastest way to find the minimum and maximum values in an array in JavaScript

Now you know all the features of math.max () and math.min ()!

Happy Coding! 😊 💻 😉 🔥 🙃

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.

The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.