Special values of JavaScript NaN and Infinity [translation]

Source: Internet
Author: User

1. NaN

In JavaScript, NaN indicates "not a number". This value is returned when an error occurs in parsing the string:Copy codeThe Code is as follows:> Number ("xyz ")
NaNNaN

The name is "not a number", but it can be said that it is not a number:Copy codeThe Code is as follows:> NaN! = NaN
True

It is a number! Type: "number"Copy codeThe Code is as follows:> typeof NaN
'Number'

1.1 check NaN
In JavaScript, NaN is the only value that you don't want to wait. therefore, you cannot use the equal sign operator to determine whether a value is NaN, but there is a global function isNaN () to do this.Copy codeThe Code is as follows:> isNaN (NaN)
True

Indicated by Kit CambridgeAn issue with isNaN (): It implicitly converts its parameter to a number, so even if the parameter is a string that cannot be converted to a number, it returnsTrue (converted to NaN):

Copy codeThe Code is as follows:> Number ("xyz ")
NaN
> IsNaN ("xyz ")
True

For the same reason, isNaN returns true for many other objects:Copy codeThe Code is as follows:> Number ({})
NaN
> IsNaN ({})
True

> Number (["xzy"])
NaN
> IsNaN (["xzy"])
True

The same is the custom object that overrides the valueOf method:Copy codeThe Code is as follows:> var obj = {valueOf: function () {return NaN }};
> Number (obj)
NaN
> IsNaN (obj)
True

So NaN can be the only one that satisfies (x! = X) the value of the inequality to write an isNaN function of its own, so that there will be no problem mentioned above:Copy codeThe Code is as follows: function myIsNaN (x ){
Return x! = X;
}

Currently, the isNaN Method Number of a revised version. isNaN () has been added to ECMAScript 6 ). crockford implements this method, which is easier to understand than myIsNaN. The core code is as follows:Copy codeThe Code is as follows: Number. isNaN = function (value ){
Return typeof value = 'number' & isNaN (value );
};

2. Infinity

Using 0 as the divisor produces another special value Infinity:Copy codeThe Code is as follows:> 3/0
Infinity

You cannot assume that the result of positive or negative infinity is:Copy codeThe Code is as follows:> Infinity-Infinity
NaN

The value larger than infinity is still infinity:

Copy codeThe Code is as follows:> Infinity + Infinity
Infinity> 5 * Infinity
Infinity

3. Reference

What is {} + {} in JavaScript?

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.