1. NaN
In JavaScript, NaN indicates "not a number". This value is returned when an error occurs in parsing the string:Copy codeThe Code is as follows:> Number ("xyz ")
NaNNaN
The name is "not a number", but it can be said that it is not a number:Copy codeThe Code is as follows:> NaN! = NaN
True
It is a number! Type: "number"Copy codeThe Code is as follows:> typeof NaN
'Number'
1.1 check NaN
In JavaScript, NaN is the only value that you don't want to wait. therefore, you cannot use the equal sign operator to determine whether a value is NaN, but there is a global function isNaN () to do this.Copy codeThe Code is as follows:> isNaN (NaN)
True
Indicated by Kit CambridgeAn issue with isNaN (): It implicitly converts its parameter to a number, so even if the parameter is a string that cannot be converted to a number, it returnsTrue (converted to NaN):
Copy codeThe Code is as follows:> Number ("xyz ")
NaN
> IsNaN ("xyz ")
True
For the same reason, isNaN returns true for many other objects:Copy codeThe Code is as follows:> Number ({})
NaN
> IsNaN ({})
True
> Number (["xzy"])
NaN
> IsNaN (["xzy"])
True
The same is the custom object that overrides the valueOf method:Copy codeThe Code is as follows:> var obj = {valueOf: function () {return NaN }};
> Number (obj)
NaN
> IsNaN (obj)
True
So NaN can be the only one that satisfies (x! = X) the value of the inequality to write an isNaN function of its own, so that there will be no problem mentioned above:Copy codeThe Code is as follows: function myIsNaN (x ){
Return x! = X;
}
Currently, the isNaN Method Number of a revised version. isNaN () has been added to ECMAScript 6 ). crockford implements this method, which is easier to understand than myIsNaN. The core code is as follows:Copy codeThe Code is as follows: Number. isNaN = function (value ){
Return typeof value = 'number' & isNaN (value );
};
2. Infinity
Using 0 as the divisor produces another special value Infinity:Copy codeThe Code is as follows:> 3/0
Infinity
You cannot assume that the result of positive or negative infinity is:Copy codeThe Code is as follows:> Infinity-Infinity
NaN
The value larger than infinity is still infinity:
Copy codeThe Code is as follows:> Infinity + Infinity
Infinity> 5 * Infinity
Infinity
3. Reference
What is {} + {} in JavaScript?