Instructions for using isNaN functions in JavaScript _javascript tips

Source: Internet
Author: User
isNaN function

Returns a Boolean value that indicates whether the supplied value is a reserved value NaN (not a number).
NaN is not a number
isNaN (NumValue)

The required NumValue parameter is the value to check for NAN.

Description

If the value is NaN, then the isNaN function returns True, otherwise it returns false. The typical case for using this function is to check the input values of the parseint and Parsefloat methods.
There is also a way in which a variable can be compared with itself. If the results of comparison are unequal, then it is NaN. This is because NaN is the only value that is unequal to itself.
Example of a isNaN function usage: For example, I have a textbox for inputting digital data, and when submitting a form I want to verify that the value in the textbox is data, then we can use the isNaN function.
Copy Code code as follows:

function CheckValue ()
{
if (isNaN (Document.all.textbox.value))
{
Alert ("Please use digital input!") ");
return false;
}
}

The typical use of isNaN is to perform an advanced test for the input values of the parseint and parsefloat methods, but this simple result is not good. Although it may be said in some textbooks, it is wrong to think that it is a return value for checking parseint and parsefloat.

Parseint and parsefloat are converts a character type to a number. But there's a problem with it, if your input values are not a number, then parseint and parsefloat return a message: "is not number", but in general we are all converted to a string of numeric styles, who would be bored to convert to a non-numeric type? So you enter the parameters are qualified, you can first conduct a isnan check.

But if we are conscious of the following behavior, in advance can not be checked by isNaN, the beginning of the number contains letters, if the isNaN to check is not pass.

If it's an empty string or a space, and the isNaN is handled as a number 0, and parseint and parsefloat are returning an error message, the isNaN check is not tight.
Two functions are independent and do not live for who, isNaN and parseint/parsefloat have no relationship.
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.