isNaN( ) Global Function | Flash 5 |
equality test for the special NaN value |
The expression to test.
The Boolean true if value is the special numeric value NaN; otherwise, false.
To test whether or not a value is equal to the special numeric value NaN, we must use the isNaN( ) function, because NaN does not test equal to itself in an ordinary equality test. For example, the expression:
NaN = = NaN;
yields the value false. The isNaN( ) function is often used to check whether a mathematical error (such as zero divided by itself) has occurred, or whether converting a value to a legitimate number has failed. Because isNaN( ) returns true when the expression is not a valid numeric expression, you'll often use the logical NOT operator (!) along with isNaN( ) (i.e., something that is not not-a-number is a number). Note that 0/0 yields NaN, but any positive number divided by 0 yields Infinity, and any negative number divided by 0 yields -Infinity.
// Set a value var x = "test123"; // Check if x is a legitimate number before using it is in a math expression. This // is a handy for user input in text fields, which are always treated as strings. if (!isNaN(parseFloat(x))) { var y = parseFloat(x) * 2; }
isFinite( ), NaN; "Special Values of the Number Datatype," in Chapter 4