
Key Object
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Alphabetical Language Reference
|
555
isNaN() Global Function
equality test for the special NaN value
Flash 5
isNaN(value)
Arguments
value The expression to test.
Returns
The Boolean true if value is the special numeric value NaN; otherwise, false.
Description
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 mathemat-
ical 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.
Example
// 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;
}
See Also ...