1.NaN
In JavaScript, Nan represents "not a number". This value is returned primarily when parsing a string error:
Copy Code code as follows:
The name is "not a number", but it can also be said not to a number:
Copy Code code as follows:
It's a number! The type is??
Copy Code code as follows:
1.1 Detection Nan
In JavaScript, Nan is a value that is only one by one and does not want to wait. Therefore, it is not possible to use the Equals operator to determine whether a value is Nan, but there is a global function isNaN () to do it.
Copy Code code as follows:
Kit Cambridge points to a problem with isNaN () : It implicitly converts its arguments to numbers, so even if the argument is a string that cannot be converted to a number, it returns True (converted to Nan):
Copy Code code as follows:
> Number ("xyz")
NaN
> isNaN ("xyz")
True
For the same reason, isNaN returns true for many other objects as well:
Copy Code code as follows:
> Number ({})
NaN
> isNaN ({})
True
> Number (["Xzy"])
NaN
> isNaN (["Xzy"])
True
The custom object that overridden the ValueOf method also:
Copy Code code as follows:
> var obj = {valueof:function () {return NaN}};
> Number (obj)
NaN
> isNaN (obj)
True
So you can use Nan is the only value that satisfies (x!== x) inequalities to write one's own isNaN function, so that there is no problem mentioned above:
Copy Code code as follows:
function Myisnan (x) {
return x!== x;
}
Currently a modified version of the isNaN Method Number.isnan () has been added to the ECMAScript 6 (translator note: Firefox has been implemented). This method of Crockford implementation is easier to understand than the Myisnan above, and the core code is this:
Copy Code code as follows:
Number.isnan = function (value) {
return typeof value = = = ' Number ' && isNaN (value);
};
2.Infinity
Dividing by 0 will produce another special value infinity:
Copy Code code as follows:
You can't take for granted the calculated result of positive infinity or negative infinity:
Copy Code code as follows:
A value larger than infinity is still infinitely large:
Copy Code code as follows:
> Infinity + Infinity
Infinity> 5 * Infinity
Infinity
3. For reference
What is {} + {} in JavaScript?