NaN (not is number) means that the integer and floating-point numbers are numbers in JS.
In addition, number has a special value, NaN.
A situation in which a Nan value may be generated
1, expression calculation,
① If an expression contains a minus sign, multiplication sign, division sign, the JS engine will convert the parts of the expression to the number type (using the number () function conversion) when calculating. If the conversion fails, return Fanan.
Example:
100 -
‘2a‘
;
// NaN
The ② plus sign (+) does not convert the variables on either side of it to the number type, because the execution order of the JS expression is performed from left to right according to the precedence of the operator, and if the variables on both sides of the plus sign (+) are of type #, the numeric addition is done if one of the variables is a string. Both sides are added as strings.
1 + 3 + ' 56 ' = 456
1 + 3 + 5 = 9
2. Type conversion
① directly converts a non-numeric expression using the parseint, parsefloat, and number functions, returning Nan.
②number is the conversion of the entire number, parseint and parsefloat are the first non-digit conversions encountered in the previous section.
For example
parseint (' 123AA ')//return 123
parsefloat (' 123aaa ')//return 123
Number (' 123aaa ')//Return Nan
3, how to determine whether a number Nan
IsNaN (parameter), this function is to determine the value of this parameter using the number function conversion, so to determine whether a variable is Nan, you cannot use this function, use the IsNaN function of the number object.
Number.isNaN(
‘123‘
);
// false 本身不是NaN
Number.isNaN(
‘abc‘
);
// false 本身不是NaN
Number.isNaN(NaN);
// true 本身是NaN
Nan and isNaN of js-