Why is NaN not equal to itself?

Source: Internet
Author: User

Nan is ' not a number ', is not a digit, so what is Nan exactly? In JavaScript, there are 6 big data types, including String,number,boolean,undefined,null and object, and for JS, integers and floating-point numbers are collectively referred to as number types, and in addition, The number type also has a very special value, Nan, which is a state that is used to indicate whether a type is of number: Yes or No. Instead of an exact value.

So, in what situations does the Nan value usually occur? There are generally two situations:

(1) If there is a minus (-), multiplication sign (*), or division sign (/) operator in an expression, the JS engine attempts to convert the variables on either side of the operator to number type before the calculation, and if the conversion fails, the expression returns Nan

(2) directly using parseint, parsefloat or number when converting a non-numeric value to a number, the expression returns Nan

"ABC"-3   //  nanparseint ("abc")  //  nanparsefloat ( // NaN Number ("abc")    // NaN

When an expression returns Nan, it indicates that there are no variables that can be converted in the expression, and returns an invalid result, not returning an exact value.

If Nan is a worthwhile word, Nan is an arbitrary value except for a number, but it is definitely not a definite value!

If you don't understand, we can also use regular expressions to describe Nan values: [^\d]

So nan! = Nan, because it is a range and cannot represent a definite value

In addition, there is a function associated with Nan, IsNaN (), which is used to determine whether a variable is a number or whether it can be converted to a number.

// false IsNaN (//falseIsNaN (//trueIsNaN ( // True

Some friends may have a misunderstanding of Nan, and I don't understand the following question:

// true  "abc" = = NaN  should also be true,

So I think isNaN () is not reliable, and then encapsulate the function to determine whether a variable is really nan, you think, Nan even do not recognize itself, you expect it and who is equal?

Speaking here, some friends are not think of undefined and null, finally understand how NaN is going, at this moment, there is wood feel more and more do not understand undefined and null, then I would like to chat about them ~

Undefined is one of 6 data types in JavaScript that has only one value, that is, undefined. Undefined means ' undefined ', that is, when a variable is declared with Var but not copied, the value of this variable is undefined. There are two reasons why undefined are produced:

(1) A property or method that accesses an object that does not exist

(2) Declaring a variable but never assigning a value

var v1,obj =//undefined//undefinedtypeof // "undefined" typeof // "undefined" typeof // "undefined"

Unlike Nan, undefined also represents a state of a variable, but the state value is determined to be unique, that is, when a variable is declared but not assigned, its state is undefined, so the following expression is true:

var//true

After understanding the undefined, it is much easier to understand NULL, and the null type has only one value: null, which means that no valid data is contained in a variable. Null here means ' null ', ' empty object ', or, more specifically, a variable that is assigned null does not hold a valid numeric value, String, Boolean, array, or object, etc., you can empty the contents of a variable by assigning NULL to a variable. There is only one reason for null: an explicit assignment to a variable is null.

var NULL  //nulltypeof//  "Object"typeofnull/ /  "Object"

Also compared to Nan, NULL is also a state value that determines tangent, and when a variable is assigned null, it is equal to NULL, so the following expression is also true:

var NULL  null//true

(For more information on JS data types, please refer to: http://www.cnblogs.com/onepixel/p/5140944.html)

@ One Pixel 2016 Blog Park

Original address: http://www.cnblogs.com/onepixel/p/5281796.html

Why is NaN not equal to itself?

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.