JavaScript Nan and Infinity Special values [translation]_javascript tips

Source: Internet
Author: User
1.NaN

In JavaScript, Nan represents "not a number". This value is returned primarily when parsing a string error:
Copy Code code as follows:

> Number ("xyz")
Nannan


The name is "not a number", but it can also be said not to a number:
Copy Code code as follows:

> Nan!== nan
True

It's a number! The type is??
Copy Code code as follows:

> typeof NaN
' Number '

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:

> isNaN (NaN)
True

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:

> 3/0
Infinity

You can't take for granted the calculated result of positive infinity or negative infinity:
Copy Code code as follows:

>infinity-infinity
NaN

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?

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.