The global NaN
property is a value representing Not-A-Number.
{{js_property_attributes(0,0,0)}}
語法
NaN
描述
NaN
is a property of the global object.
The initial value of NaN
is Not-A-Number — the same as the value of {{jsxref("Number.NaN")}}. In modern browsers, NaN
is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it.
It is rather rare to use NaN
in a program. It is the returned value when {{jsxref("Math")}} functions fail (Math.sqrt(-1)
) or when a function trying to parse a number fails (parseInt("blabla")
).
Testing against NaN
NaN
compares unequal (via ==
, !=
, ===
, and !==
) to any other value -- including to another NaN value. Use {{jsxref("Number.isNaN()")}} or {{jsxref("Global_Objects/isNaN", "isNaN()")}} to most clearly determine whether a value is 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
規範
Specification | Status | Comment |
---|---|---|
{{SpecName('ES1')}} | {{Spec2('ES1')}} | Initial definition. Implemented in JavaScript 1.3 |
{{SpecName('ES5.1', '#sec-15.1.1.1', 'NaN')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-value-properties-of-the-global-object-nan', 'NaN')}} | {{Spec2('ES6')}} | |
{{SpecName('ESDraft', '#sec-value-properties-of-the-global-object-nan', 'NaN')}} | {{Spec2('ESDraft')}} |
瀏覽器相容性
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
參見
- {{jsxref("Number.NaN")}}
- {{jsxref("Number.isNaN()")}}
- {{jsxref("isNaN", "isNaN()")}}