My friends and family are under attack in Ukraine. Donate to protect them directly or help international organizations.

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

April 2nd, 2009

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.

Previous: Flex 3D and Wiimote Next: Application Domain and External SWF Loading