NaN
全域屬性 NaN
表示「非數值」(Not-A-Number)的數值。
Property attributes of NaN |
|
---|---|
Writable | no |
Enumerable | no |
Configurable | no |
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
語法
NaN
描述
NaN
的屬性屬於全域物件。
如同 Number.NaN
(en-US) 一般,NaN
的初始數值是「非數值」。在當今的瀏覽器中,NaN
屬性不可設定(non-configurable)也不可覆寫(non-writable)。雖然可能有例外,也請不要覆蓋它。
寫程式很少會直接動用 NaN
。通常是在 Math
函式計算失敗(Math.sqrt(-1)
)或函式解析數字失敗(parseInt("blabla")
)後才會回傳。
偵測是否為 NaN
NaN
不等於(==
、!=
、===
、!==
)任何值,包括 NaN 本身。請使用 Number.isNaN()
或 isNaN()
來確認某個數值是否為 NaN。Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.
NaN === NaN; // false
Number.NaN === NaN; // false
isNaN(NaN); // true
isNaN(Number.NaN); // true
function valueIsNaN(v) { return v !== v; }
valueIsNaN(1); // false
valueIsNaN(NaN); // true
valueIsNaN(Number.NaN); // true
但請注意 isNaN()
與 Number.isNaN()
之間是有區別的:前者會在目前數字是 NaN
的時候回傳 true
,或在裡面包藏一個號碼後變成 NaN
;而後者,只有在數值是 NaN
的時候才會回傳 true
。
規範
規範 | 狀態 | 註解 |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | 初始定義。JavaScript 1.3 導入 |
ECMAScript 5.1 (ECMA-262) The definition of 'NaN' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'NaN' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'NaN' in that specification. |
Living Standard |
瀏覽器相容性
BCD tables only load in the browser