isNaN( ) Global Function

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
isNaN( ) Global Function Flash 5

equality test for the special NaN value
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 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.

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

isFinite( ), NaN; "Special Values of the Number Datatype," in Chapter 4



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net