Nan and isNaN of js-

Source: Internet
Author: User

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-

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.