Wednesday, July 28, 2010

empty strings are (not) different then false

Did you know that if you are testing if a value does not equal an empty string in javascript:

v!=''

might return somewhat unexpected results if v is false



var v=false;
alert(v!='')


will alert false and not true as you might expect.
At first glance I said, well o.k. this does make sense because v is a boolean so the empty string is being cast to a boolean for the comparison yielding false.

so I tried


var v=false;
alert(''!=v)


I expected that now the false value of v will be cast to the 'false' string which obviously is not an empty string and that the alert will now show true.

but that does not work either.

finally:

var v=false;
alert(''!=String(v))


does work as expected and results in true being returned.

I wander if this is also the case in ActionScript ...

No comments:

 
Clicky Web Analytics