Usage and instance of JavaScript Nan

Source: Internet
Author: User
Tags numeric numeric value

Nan is a value type, and it is also a number. It means not a number, and this all knows what it means. The value is special, especially if Nan is a digit and is a number that is not equal to any number.

Three NaN values
is a special value that represents the non-number (not a numbers)
Type conversion fails to return Nan
e.g. to convert a word blue to a numeric value will fail because there is no equivalent value
NaN does not equal himself
That is, Nan = = Nan is False
Judge Nan using isNaN ();


Web effects: Alert (typeof (NaN)); You can fully verify that this is a numeric type value. That is, NaN was a strategy of error at the time of conversion and turned into a number that no one knew. From this point of view it is a number, Can be understood as infinity or infinitesimal in mathematics. But contrary to a theory in mathematics, we often say that infinity plus 1 equals infinity is wrong. It is no empty big plus 1 is not equal to infinity, so infinity is not the same as itself. So the value Nan is not equal to itself.

And in JavaScript, Nan is an infinity, then it represents no longer a value, but a class of values, so sometimes visual nan is a type.

It is this edge problem that can cause the following tests to be correct:

Javascript:alert (nan+1); the result is infinity.
But the only Nan is an indeterminate number, so Nan cannot equal Nan.

This is like Random,random +1 in randomise or an indeterminate number, but random is not equal to random. The difference is that random can generate a certain number, but it is not certain. And Nan generates an indeterminate number.


Definitions and usage
The NaN property is used to refer to special non-numeric values.

Grammar
Nan Description
You cannot use the For/in loop to enumerate the NaN property, nor can you delete it by using the delete operator.

NaN is not a constant and can be set to a different value.
Tips and comments
Tip: Use isNaN () to determine whether a value is a number. The reason is that NaN is not equal to all values, including itself.
Instance
In this case, we'll show what happens when a number exceeds the limit of the Infinity:

<script type= "Text/javascript" >

var test1= "300"
var test2= "Hello world!"

document.write (number (test1) + "<br/>")
document.write (number (test2) + "<br/>")

document.write (isNaN (test1) + "<br/>")
document.write (isNaN (test2))

</script> output:

300NaNfalsetrue

Instance

<body>
<script language= "JavaScript" >
<!--
if (123 = = Number.NaN) {
document.write ("This isn't a number");
}
-->
</script>

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.