考慮以下代碼:javascript
var min = Math.min(); var max = Math.max(); console.log(min < max);
按照常規思路,這段代碼應該輸出 true
,畢竟最小值應該小於最大值。可是當咱們運行這段代碼時,卻神奇的輸出了 false
。java
爲何會這樣呢?瀏覽器
還得去查查 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
若是沒有參數,則返回 Infinity
。Infinity
是什麼呢?Infinity
是 javascript 中全局對象的一個屬性,在瀏覽器環境中就是 window
對象的一個屬性,表示無窮大。io
而 Math.max()
沒有傳遞參數時返回的是 -Infinity
。
所以 Math.min()
要比 Math.max()
大。