isNaN()

isNaN() 函数确定某个值是否为 NaN,如有必要,首先将该值转换为数字。因为 isNaN() 函数内部的强制转换可以是 surprising,所以你可能更喜欢使用 Number.isNaN()

¥The isNaN() function determines whether a value is NaN, first converting the value to a number if necessary. Because coercion inside the isNaN() function can be surprising, you may prefer to use Number.isNaN().

Try it

语法

¥Syntax

js
isNaN(value)

参数

¥Parameters

value

要测试的值。

返回值

¥Return value

如果给定值为 转换为数字 后为 NaN,则为 true;否则,false

¥true if the given value is NaN after being converted to a number; otherwise, false.

描述

¥Description

isNaN() 是全局对象的函数属性。

¥isNaN() is a function property of the global object.

对于数字值,isNaN() 测试该数字是否为值 NaN。当 isNaN() 函数的参数不是 数字 类型时,该值首先被强制转换为数字,然后将结果值与 NaN 进行比较。

¥For number values, isNaN() tests if the number is the value NaN. When the argument to the isNaN() function is not of type Number, the value is first coerced to a number, and the resulting value is then compared against NaN.

isNaN() 对于非数字参数的这种行为可能会令人困惑!例如,空字符串被强制为 0,而布尔值被强制为 0 或 1;直观上,这两个值都是 "不是数字",但它们的计算结果不是 NaN,因此 isNaN() 返回 false。因此,isNaN() 既不回答问题“输入的是浮点 NaN 值”,也不回答问题 "输入不是数字"。

¥This behavior of isNaN() for non-numeric arguments can be confusing! For example, an empty string is coerced to 0, while a boolean is coerced to 0 or 1; both values are intuitively "not numbers", but they don't evaluate to NaN, so isNaN() returns false. Therefore, isNaN() answers neither the question "is the input the floating point NaN value" nor the question "is the input not a number".

Number.isNaN() 是测试某个值是否为数值 NaN 的更可靠方法。或者,可以使用表达式 x !== x,并且这两种解决方案都不会受到导致全局 isNaN() 不可靠的误报的影响。要测试某个值是否为数字,请使用 typeof x === "number"

¥Number.isNaN() is a more reliable way to test whether a value is the number value NaN or not. Alternatively, the expression x !== x can be used, and neither of the solutions is subject to the false positives that make the global isNaN() unreliable. To test if a value is a number, use typeof x === "number".

isNaN() 函数回答了“当在数字上下文中使用时输入在功能上等同于 NaN”的问题。如果 isNaN(x) 返回 false,你可以在算术表达式中使用 x,就好像它是除 NaN 以外的有效数字一样。如果 isNaN(x) 返回 truex 将被强制转换为 NaN,并使大多数算术表达式返回 NaN(因为 NaN 传播)。例如,你可以使用它来测试函数的参数是否可以进行算术处理(可用 "like" 数字),并通过抛出错误、提供默认值等来处理不是数字的值。这样, 你可以拥有一个函数,该函数通过根据上下文隐式转换值来利用 JavaScript 提供的完整多功能性。

¥The isNaN() function answers the question "is the input functionally equivalent to NaN when used in a number context". If isNaN(x) returns false, you can use x in an arithmetic expression as if it's a valid number that's not NaN. If isNaN(x) returns true, x will get coerced to NaN and make most arithmetic expressions return NaN (because NaN propagates). You can use this, for example, to test whether an argument to a function is arithmetically processable (usable "like" a number), and handle values that are not number-like by throwing an error, providing a default value, etc. This way, you can have a function that makes use of the full versatility JavaScript provides by implicitly converting values depending on context.

注意:+ 运算符 执行数字加法和字符串连接。因此,即使 isNaN() 对于两个操作数都返回 false+ 运算符仍可能返回字符串,因为它不用作算术运算符。例如,isNaN("1") 返回 false,但 "1" + 1 返回 "11"。为了确保你正在使用数字,将值强制为数字 并使用 Number.isNaN() 来测试结果。

¥Note: The + operator performs both number addition and string concatenation. Therefore, even if isNaN() returns false for both operands, the + operator may still return a string, because it's not used as an arithmetic operator. For example, isNaN("1") returns false, but "1" + 1 returns "11". To be sure that you are working with numbers, coerce the value to a number and use Number.isNaN() to test the result.

示例

¥Examples

请注意,对于既不是值 NaN 但也不是数字的值,isNaN() 如何返回 true

¥Note how isNaN() returns true for values that are not the value NaN but are not numbers either:

js
isNaN(NaN); // true
isNaN(undefined); // true
isNaN({}); // true

isNaN(true); // false
isNaN(null); // false
isNaN(37); // false

// Strings
isNaN("37"); // false: "37" is converted to the number 37 which is not NaN
isNaN("37.37"); // false: "37.37" is converted to the number 37.37 which is not NaN
isNaN("37,5"); // true
isNaN("123ABC"); // true: Number("123ABC") is NaN
isNaN(""); // false: the empty string is converted to 0 which is not NaN
isNaN(" "); // false: a string with spaces is converted to 0 which is not NaN

// Dates
isNaN(new Date()); // false; Date objects can be converted to a number (timestamp)
isNaN(new Date().toString()); // true; the string representation of a Date object cannot be parsed as a number

// Arrays
isNaN([]); // false; the primitive representation is "", which coverts to the number 0
isNaN([1]); // false; the primitive representation is "1"
isNaN([1, 2]); // true; the primitive representation is "1,2", which cannot be parsed as number

规范

Specification
ECMAScript Language Specification
# sec-isnan-number

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also