How to Use isNaN functions in Javascript _ javascript skills

Source: Internet
Author: User
The isNaN function returns a Boolean value indicating whether the provided value is a reserved value, NaN (not a number ). IsNaN Function

Returns a Boolean value indicating whether the provided value is a reserved value NaN (not a number ).
NaN: Not a Number
IsNaN (numValue)

The required numvalue parameter is the value to check whether it is NAN.

Description

If the value is NaN, The isNaN function returns true; otherwise, false. This function is typically used to check the input values of the parseInt and parseFloat methods.
Another way is to compare variables with itself. If the comparison result is different, it is NaN. This is because NaN is the only value that is different from itself.
Example of isNaN function usage: for example, if I have a textbox used to input numeric data, and I want to verify whether the value in the textbox is data when submitting a form, we can use the isNaN function.

The Code is as follows:


Function checkValue ()
{
If (isNaN (document. all. textbox. value ))
{
Alert ("enter a number! ");
Return false;
}
}


The typical use of isNaN is to make an advance test for the input values of parseInt and parseFloat methods, but this simple result is not good. Although it may also be said in some teaching books, or even mistakenly think it is a return value for checking parseInt and parseFloat, this is all wrong.

ParseInt and parseFloat convert a numeric value. However, you still have error processing. If your input value is not a number, parseInt and parseFloat return a message: "is not number ", but in general, we convert a numeric string. Who will be bored with converting a non-numeric string? Therefore, if the parameter you entered is qualified, you can perform an isNaN check first.

However, if we are conscious of the following actions, we cannot use isNaN for checking beforehand. If isNaN is used for checking if the number starts with a letter, it is not acceptable.

If it is an empty string or a space, and isNaN is processed as a number 0, and parseInt and parseFloat return an error message, this isNaN check is not strict.
The two functions are independent and will not survive for anyone. isNaN has nothing to do with parseInt/parseFloat.

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.