Math.min() 为何比 Math.max() 大?

考虑以下代码:javascript

var min = Math.min();
var max = Math.max();
console.log(min < max);

按照常规思路,这段代码应该输出 true,毕竟最小值应该小于最大值。可是当咱们运行这段代码时,却神奇的输出了 falsejava

为何会这样呢?浏览器

还得去查查 MDN 的相关文档。code

The Math.min() function returns the smallest of zero or more numbers.对象

Math.min 的参数是 0 个或者多个。若是是多个参数很容易理解,返回参数中最小的。ip

若是是 0 个参数呢?文档中写到:文档

If no arguments are given, the result is Infinity.get

If at least one of arguments cannot be converted to a number, the
result is NaN.it

若是没有参数,则返回 InfinityInfinity 是什么呢?Infinity 是 javascript 中全局对象的一个属性,在浏览器环境中就是 window 对象的一个属性,表示无穷大。io

Math.max() 没有传递参数时返回的是 -Infinity

所以 Math.min() 要比 Math.max() 大。

继续阅读:为何 Math.min() 比 Math.max() 大?(续)

相关文章
相关标签/搜索