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() 大?(續)

相關文章
相關標籤/搜索