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.