Name
isNaN( ) Global Function — equality test for the special NaN value
Availability
Flash 5
Synopsis
isNaN(value)
Arguments
- value
The expression to test.
Returns
A Boolean value: 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 mathematical
error (such as zero divided by itself) has occurred in a phrase of
code 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( )
(something that is not not a number
is a number). Note that 0/0 yields
NaN, but all positive numbers divided by
yield Infinity, and all negative numbers divided
by
yield -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 technique for user input in text fields, which always a string.
if (!isNaN(parseFloat(x))) {
var y = parseFloat(x) * 2;
}See Also
isFinite( ), NaN; Section 4.3.3 in Chapter 4
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access