Number.POSITIVE_INFINITY
Number.POSITIVE_INFINITY 靜態資料屬性表示正無窮大值。
試一試
function checkNumber(bigNumber) {
if (bigNumber === Number.POSITIVE_INFINITY) {
return "Process number as Infinity";
}
return bigNumber;
}
console.log(checkNumber(Number.MAX_VALUE));
// Expected output: 1.7976931348623157e+308
console.log(checkNumber(Number.MAX_VALUE * 2));
// Expected output: "Process number as Infinity"
值
與全域性 Infinity 屬性的值相同。
Number.POSITIVE_INFINITY 的屬性特性 | |
|---|---|
| 可寫 | 否 |
| 可列舉 | 否 |
| 可配置 | 否 |
描述
Number.POSITIVE_INFINITY 的值與數學上的無窮大行為略有不同
- 任何正值(包括
POSITIVE_INFINITY)乘以POSITIVE_INFINITY都等於POSITIVE_INFINITY。 - 任何負值(包括
NEGATIVE_INFINITY)乘以POSITIVE_INFINITY都等於NEGATIVE_INFINITY。 - 任何正數除以
POSITIVE_INFINITY都等於 正零(由 IEEE 754 定義)。 - 任何負數除以
POSITIVE_INFINITY都等於 負零(由 IEEE 754 定義。 - 零乘以
POSITIVE_INFINITY等於NaN。 NaN乘以POSITIVE_INFINITY等於NaN。POSITIVE_INFINITY除以任何負值(不包括NEGATIVE_INFINITY)等於NEGATIVE_INFINITY。POSITIVE_INFINITY除以任何正值(不包括POSITIVE_INFINITY)等於POSITIVE_INFINITY。POSITIVE_INFINITY除以NEGATIVE_INFINITY或POSITIVE_INFINITY等於NaN。- 對於任何不是
POSITIVE_INFINITY的數字 x,Number.POSITIVE_INFINITY > x為真。
您可以使用 Number.POSITIVE_INFINITY 屬性來指示一個錯誤條件,該條件在成功時返回一個有限的數字。但請注意,在這種情況下,NaN 會更合適。
因為 POSITIVE_INFINITY 是 Number 的一個靜態屬性,所以您始終將其作為 Number.POSITIVE_INFINITY 使用,而不是作為數字值的屬性。
示例
使用 POSITIVE_INFINITY
在下面的示例中,變數 bigNumber 被賦予了一個大於最大值的數值。當 if 語句執行時,bigNumber 的值為 Infinity,因此 bigNumber 在繼續之前被設定為一個更易於管理的值。
js
let bigNumber = Number.MAX_VALUE * 2;
if (bigNumber === Number.POSITIVE_INFINITY) {
bigNumber = returnFinite();
}
規範
| 規範 |
|---|
| ECMAScript® 2026 語言規範 # sec-number.positive_infinity |
瀏覽器相容性
載入中…