We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS


是非お気軽に MDN に登録して翻訳に参加し、私たちの手助けをして下さい!



実装されたバージョン JavaScript 1.0
ECMAScript エディション ECMAScript 1st Edition






isNaN は、トップレベルの関数であり、何のオブジェクトとも関連付けられていません。

isNaN は、渡された引数を数に変換できるかどうかを試みます。その引数が変換できなかった場合、true を返します。そうでない場合は、false を返します。

この関数が役立つ理由は、NaN の値は、同値演算子では、意味があるテストが成されないからです。 x == NaNx === NaN は、常に false です。x が何であるかに関わらず、 たとえ xNaN であっても、です。例えば、1 == NaNNaN == NaN は、両方とも、false を返します。

The necessity of an isNaN function

Unlike all other possible values in JavaScript, it is not possible to rely on the equality operators (== and ===) to determine whether a value is NaN or not, because both NaN == NaN and NaN === NaN evaluate to false. Hence, the necessity of an isNaN function.

Origin of NaN values

NaN values are generated when arithmetic operations result in undefined or unrepresentable values. Such values do not necessarily represent overflow conditions. A NaN also results from attempted coercion to numeric values of non-numeric values for which no primitive numeric value is available.

For example, dividing zero by zero results in a NaN — but dividing other numbers by zero does not.

Confusing special-case behavior

Since the very earliest versions of the isNaN function specification, its behavior for non-numeric arguments has been confusing. When the argument to the isNaN function is not a number, the value is first coerced to a number. The resulting value is then tested to determine whether it is NaN. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one), the "false" returned value may be unexpected; the empty string, for example, is surely "not a number." The confusion stems from the fact that the term, "not a number", has a specific meaning for numbers represented as IEEE-794 floating-point values. The function should be interpreted as answering the question, "is this value, when coerced to a numeric value, an IEEE-794 'Not A Number' value?"

The next version of ECMAScript (ES6) is likely to contain an Object.is function. Object.is(x, NaN) will be a reliable way to test whether x is NaN or not.

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("");        // 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

// This is a false positive and the reason why isNaN is not entirely reliable
isNaN("blabla")   // true: "blabla" is converted to a number. Parsing this as a number fails and returns NaN



最終更新者: Potappo,