此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Number.NaN

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Number.NaN 静态数据属性表示非数字值,等同于 NaN。有关 NaN 的行为的更多信息,请参阅全局属性的描述

尝试一下

function clean(x) {
  // eslint-disable-next-line use-isnan
  if (x === Number.NaN) {
    // Can never be true
    return null;
  }
  if (isNaN(x)) {
    return 0;
  }
}

console.log(clean(Number.NaN));
// Expected output: 0

数字值 NaN

Number.NaN 的属性特性
可写
可枚举
可配置

描述

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

示例

检查值是否为数字

js
function sanitize(x) {
  if (isNaN(x)) {
    return Number.NaN;
  }
  return x;
}

规范

Specification
ECMAScript® 2026 Language Specification
# sec-number.nan

浏览器兼容性

参见