Number.NEGATIVE_INFINITY

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Number.NEGATIVE_INFINITY 静态数据属性表示负无穷值。

尝试一下

function checkNumber(smallNumber) {
  if (smallNumber === Number.NEGATIVE_INFINITY) {
    return "Process number as -Infinity";
  }
  return smallNumber;
}

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.NEGATIVE_INFINITY 的属性特性
可写
可枚举
可配置

描述

Number.NEGATIVE_INFINITY 的表现同数学上的无穷大略有不同:

  • 任何正值,包括 POSITIVE_INFINITY,乘以 NEGATIVE_INFINITY 等于 NEGATIVE_INFINITY
  • 任何负值,包括 NEGATIVE_INFINITY,乘以 POSITIVE_INFINITY 等于 POSITIVE_INFINITY
  • 任何正值除以 NEGATIVE_INFINITY 都是负零(正如 IEEE 754 中所定义的)。
  • 任何负值除以 NEGATIVE_INFINITY 都是正零(正如 IEEE 754 中所定义的)。
  • 零除以 NEGATIVE_INFINITY 等于 NaN
  • NaN 乘以 NEGATIVE_INFINITY 等于 NaN
  • NEGATIVE_INFINITY,除以任何负值(除了 NEGATIVE_INFINITY),都等于 POSITIVE_INFINITY
  • POSITIVE_INFINITY,除以任何正值(除了 POSITIVE_INFINITY),都等于 NEGATIVE_INFINITY
  • NEGATIVE_INFINITY,除以 NEGATIVE_INFINITYPOSITIVE_INFINITY,都等于 NaN
  • x > Number.NEGATIVE_INFINITY 对于任何不是 NEGATIVE_INFINITY 的数字 x 都为真。

你可以使用 Number.NEGATIVE_INFINITY 属性来表示成功时返回有限数值的错误条件。不过,NaN 更适合于这种情况。

由于 NEGATIVE_INFINITYNumber 的静态属性,你应该始终将其用作 Number.NEGATIVE_INFINITY,而不是作为一个数字值的属性。

示例

使用 NEGATIVE_INFINITY

在下面的例子中,变量 smallNumber 被赋值为比最小值更小的数值。当 if 语句执行时,smallNumber 的值为 -Infinity,所以在继续之前,smallNumber 被设置为一个更易于管理的值。

js
let smallNumber = -Number.MAX_VALUE * 2;

if (smallNumber === Number.NEGATIVE_INFINITY) {
  smallNumber = returnFinite();
}

规范

Specification
ECMAScript® 2025 Language Specification
# sec-number.negative_infinity

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
NEGATIVE_INFINITY

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见