integer comparision with NaN


Hi,
You may be shocked while comparing a integer variable with NaN like this because default value of a integer is 0

var a:int;

if(a == NaN)
{
// code when a is not initilize
}

This will not provide desired result.


var a:int;
if(isNaN(a))
{
// code when a is not initilize
}

This will also not provide desired result.

Try this code

var a:int;
trace(a);//0
a = 10;
trace(a)//10
a = undefined;
trace(a)//0

Moral of the story: Integer number does not have NaN.

for more info see lukasz comment below

2 responses to “integer comparision with NaN

  1. Hi,
    Sorry to shock you, but you are wrong, your code will not work simply because an integer doesn’t have NotANumber so the code:

    var a:int;

    if (isNaN(a))
    {
    trace (“undefined”);
    }
    else
    {
    trace(“not undefined”);
    }
    //will return “not undefined”;

    Use Number if you rely on NaN otherwise setup yuor integer to defined value and check against it as follows:
    var a:int = int.MIN_VALUE;

    if (a == int.MIN_VALUE)
    {
    trace (“undefined”);
    }
    else
    {
    trace(“not undefined”);
    }
    //now it will return “undefined”

    best regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s