Home > Web Front-end > JS Tutorial > body text

JavaScript NaN and Infinity special values ​​[Translation]_javascript tips

WBOY
Release: 2016-05-16 17:49:49
Original
1368 people have browsed it
1.NaN

In JavaScript, NaN represents "not a number". This value is mainly returned when an error occurs in parsing a string:
Copy code The code is as follows:

> Number("xyz")
NaNNaN

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

> NaN !== NaN
true

It is a number! Type is "number"
Copy code The code is as follows:

> typeof NaN
'number'

1.1 Detecting NaN
In JavaScript, NaN is the only value that you don’t want to wait for. Therefore, you cannot use the equal sign operator to determine whether a value is NaN, but there is the global function isNaN() to do so. Do this.
Copy code The code is as follows:

> isNaN(NaN)
true

Kit Cambridge pointed out a problem with isNaN(): it will implicitly convert its parameter into a number, so even if the parameter is a string that cannot be converted into a number, It will also return true (converted to NaN):

Copy code The code is as follows:

> Number("xyz")
NaN
> isNaN("xyz")
true


Due to the same The reason is that isNaN also returns true for many other objects:
Copy code The code is as follows:

> Number({})
NaN
> isNaN({})
true

> Number(["xzy"])
NaN
> isNaN(["xzy"])
true

The same custom object that overrides the valueOf method:
Copy code The code is as follows:

> var obj = { valueOf: function () { return NaN } };
> Number(obj)
NaN
> isNaN(obj)
true

So you can use NaN to be the only value that satisfies the (x !== x) inequality to write your own isNaN function, so that There will be no problems mentioned above:
Copy code The code is as follows:

function myIsNaN( x) {
return x !== x;
}

Currently a revised version of the isNaN method Number.isNaN() has been added to ECMAScript 6 (Translator's Note: Firefox has already implemented it). This method implemented by Crockford is easier to understand than myIsNaN above. The core code is as follows:
Copy code The code is as follows:

Number.isNaN = function (value) {
return typeof value === 'number' && isNaN(value);
};


2.Infinity

Using 0 as a divisor will produce another special value Infinity:
Copy the code The code is as follows:

> 3/0
Infinity

You can’t just guess positive infinity or negative Calculation result of infinity:
Copy code The code is as follows:

>Infinity - Infinity
NaN

A value larger than infinity is still infinity:

Copy code The code is as follows:

> Infinity Infinity
Infinity> 5 * Infinity
Infinity


3.参考

What is {} {} in JavaScript?
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!