Home > AS3 Mystery: null, false, 0, “”, NaN

AS3 Mystery: null, false, 0, “”, NaN

This week I’ve been running unit tests and couldn’t understand why some of them were failing for no obvious reason. To understand why some expressions evaluated to true when they were supposed to be false. I know that I should be careful with null vs false values, but never could I have imagined that the Number 0 can be equal to an empty String!

I know that php’s empty() function is quite permissive, but I though AS3 was much more strict (with strict typing and all). And so I ran a different kind of test, where I checked these values in depth. Here is a table describing my results.

null false 0 “” NaN
isNaN() true
== “” true true true
=== “” true
== null true
=== null true
== 0 true true true
=== 0 true
== false true true true
=== false true
is Number true true
is String true
is Boolean true
is Object true true true true

You will notice that NaN is actually a Number and an Object, that 0 == “” == false and that false is actually an Object. There are quite a few surprises in there that will make me appreciate strict comparison.

  1. Michael
    August 3rd, 2009 at 13:45 | #1

    It’s sort of depressing that false is an object. Kind of makes you feel that AS3 is really dirty under the hood.

  2. Leonid
    October 7th, 2009 at 14:54 | #2

    Thank you very much for publishing this table. This will help me to code neater :)

  3. Ilya
    June 24th, 2012 at 08:45 | #3

    Yeah, it’s been a while since original post date, but still..

    You haven’t check string “0″, but it gives pretty interesting results:
    Boolean(“0″) gives us true;

    but in the same time:
    (“0″ == false) gives us true.

    And if we just use condition:
    if(“0″) trace(“Blah, blah”) // it traced “Blah, blah”.

    What a wonderful world!

    P.S. Strings “false” and “null” are normal strings and behave like normal strings.

  4. June 27th, 2012 at 14:10 | #4

    Thanks for sharing this. I found that posts are never too old. I will update this one with additional values when I get the chance.

  1. No trackbacks yet.
April Fools’ Prank on my CTO