Understanding correction of attribute nan and discrimination of Number.isnan () and IsNaN () method

Source: Internet
Author: User
Tags square root

1. Misunderstanding correction of attribute Nan

Nan (not a number) defines a non-numeric special value in the list of numbers, and it has an object that does not have the value of any non-numeric type equal to Nan, only if the arithmetic operation or data type conversion error is Nan "shows that some arithmetic operations, such as the square root of a negative number, are not numbers. methods parseint () and parsefloat () return the value Nan when the specified string cannot be resolved. For some functions that return a valid number in general, you can also use this method to Number.NaN the error condition ". NaN's comparison with other numeric values is always unequal, including itself, cannot be judged by = =, = = =, can only be called IsNaN () to compare

eg

(

The fact that means isn't a number does not mean this anything that's not a number is NaN a NaN .

NaNis a special value on floating point arithmethic this represents an undefined the result of an operation.

)

Analysis of the 2.number.isnan () and IsNaN () Methods (Number.isnan () is different from the global IsNaN () function. )

Pit: (nan is a hole in JavaScript that returns Nan when a non-numeric string is converted to a numeric type, which is supposed to return true if the string is not a numeric type with isNaN (), so isNaN () is still in the pit ), Number.isnan () corrected the bug)

(

The Global IsNaN () function converts the tested value to a number and then tests it. "

  if value can not conver< Span class= "Typ" >t to  Number, return True< Span class= "pun". else if number arithmethic result is nan< Span class= "pun" >, return true otherwise, return< Span class= "PLN" > false         

The global Method isNaN () converts the parameter to the number type and determines whether it is Nan, so the type conversion fails or the operation error value is Nan, and all others are false

Number.isnan () does not convert the values to a number, and would r Eturn false for any value that's not ofT He type number "

  if  Type (number)  is not , return false< Span class= "pun". if number is nan, return true.otherwise, return Span class= "KWD" >false            

" Number.isnan () evaluates the parameter type first, the operation error value is Nan returns True when the parameter is number, and all others are false

Tip: In JavaScript, the value NaN is considered a type of number.

)

Number.isnan () can use parseint or paresefloat () as the global Method IsNaN () to type-convert first

3. Error-Prone obfuscation examples

isnan (//trueisnan (undefined); //trueisnan ({}); //trueisnan (true); //falseisnan (null); //falseisnan (1); //false                   

isnan ( "1"); //fales  "1" is converted to digital 1, so return to falseisnan (//true  "SegmentFault" is converted to digital nan 
Number.isnan (' 0/0 ')//string not number Falseisnan (' 0/0 ')//arithmethic ilegal (NaN) Truenumber.isnan (' 123 ')//string Not number Falseisnan (' 123 ')//convert to number Falsenumber.isnan (' hello ')//string not number Falseisnan (' Hello ')//con Vert fail (NaN) Truenumber.isnan (")/isnan (NULL)//string not number Falsenumber.isnan (true)//bool not number Falseisnan (')/isnan (null)//convert to 0 Falseisnan (true)//convert to 1 Falsenumber.isnan (undefined)//undefined not number flase IsNaN (undefined)//convert fail True//convert fail Trueisnan (parseint (undefined)) IsNaN (parseint (null)) IsNaN ( parseint (")) IsNaN (parseint (True)) Number.isnan (' Nan ')//falseisnan (' Nan ')//truenumber.isnan (Nan)//trueisnan (nan )//true

  

 

Summary: Give me the feeling that in the actual use of IsNaN () to determine whether the number Number.isnan () is used to determine whether the operation is legitimate, so the general use of the global IsNaN, grasp the two methods when the focus on its algorithm logic (2nd: Type conversions or type judgments first)

Understanding correction of attribute nan and discrimination of Number.isnan () and IsNaN () method

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.