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.
It’s sort of depressing that false is an object. Kind of makes you feel that AS3 is really dirty under the hood.
Thank you very much for publishing this table. This will help me to code neater
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.
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.