1.NaN
In JavaScript, NaN represents "not a number". This value is mainly returned when an error occurs in parsing a string:
> Number("xyz")
NaNNaN
The name of
is "not a number", but it can also be said to be not not a number:
> NaN !== NaN
true
It is a number! Type is "number"
> typeof NaN
'number'
1.1 Detecting NaN In JavaScript, NaN is the only value that you don’t want to wait for. Therefore, you cannot use the equal sign operator to determine whether a value is NaN, but there is the global function isNaN() to do so. Do this.
> isNaN(NaN)
true
Kit Cambridge pointed out a problem with isNaN(): it will implicitly convert its parameter into a number, so even if the parameter is a string that cannot be converted into a number, It will also return true (converted to NaN):
> Number("xyz")
NaN
> isNaN("xyz")
true
Due to the same The reason is that isNaN also returns true for many other objects:
> Number({})
NaN
> isNaN({})
true
> Number(["xzy"])
NaN
> isNaN(["xzy"])
true
The same custom object that overrides the valueOf method:
> var obj = { valueOf: function () { return NaN } };
> Number(obj)
NaN
> isNaN(obj)
true
So you can use NaN to be the only value that satisfies the (x !== x) inequality to write your own isNaN function, so that There will be no problems mentioned above:
function myIsNaN( x) {
return x !== x;
}
Currently a revised version of the isNaN method Number.isNaN() has been added to ECMAScript 6 (Translator's Note: Firefox has already implemented it). This method implemented by Crockford is easier to understand than myIsNaN above. The core code is as follows:
Number.isNaN = function (value) {
return typeof value === 'number' && isNaN(value);
};
2.Infinity
Using 0 as a divisor will produce another special value Infinity:
> 3/0
Infinity
You can’t just guess positive infinity or negative Calculation result of infinity:
>Infinity - Infinity
NaN
A value larger than infinity is still infinity:
> Infinity Infinity
Infinity> 5 * Infinity
Infinity
3.参考
What is {} {} in JavaScript?