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

Instructions for using isNaN function in Javascript_javascript skills

WBOY
Release: 2016-05-16 17:59:42
Original
1968 people have browsed it
isNaN function

Returns a Boolean value indicating whether the supplied value is a reserved value NaN (not a number).
NaN means Not a Number
isNaN(numValue)

Required option The numvalue parameter is the value to be checked whether it is NAN.

Explanation

If the value is NaN, then the isNaN function returns true, otherwise it returns false. A typical use case for this function is to check the input values ​​of the parseInt and parseFloat methods.
Alternatively, a variable can be compared with itself. If the result of the comparison is not equal, it is NaN . This is because NaN is the only value that is not equal to itself.
IsNaN function usage example: For example, I have a textbox for entering numeric data. When submitting the form, I want to verify whether the value in the textbox is data, then we can use the isNaN function.
Copy code The code is as follows:

function checkValue()
{
if (isNaN(document.all.textbox.value))
{
alert("Please enter numbers!");
return false;
}
}

The typical use of isNaN is to perform an early check on the input value of the parseInt and parseFloat methods, but this simple result is not good. Although some textbooks may say so, and even mistakenly believe that it is a return value for checking parseInt and parseFloat, this is wrong.

parseInt and parseFloat convert a character type into a number. But it also has its own error handling. If your input value is not a number, parseInt and parseFloat return a message: "is not number", but generally we convert a numeric string. , who would be bored enough to convert non-digital ones? Therefore, you can first perform an isNaN check to see if the parameters you entered are qualified.

But if we consciously do the following behavior, we cannot use isNaN to check in advance. When there are letters at the beginning of the number, if we use isNaN to check, it will not pass.

If it is an empty string or a space, isNaN is processed as the number 0, and parseInt and parseFloat return an error message. This isNaN check is not strict and is caused by it.
The two functions are independent, and no one lives for the other. isNaN has no relationship with parseInt/parseFloat.
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!