Casting string to boolean … A little quirky….

I just stumbled across a little type conversion quirk within Flex. I haven’t tested this in-depth, so I may yet be proven wrong.

However….check this out:

var someVal:string = “true”;
var bool:Boolean;
bool = someVal as Boolean; // bool == false;
bool = someVal; // bool == true;

If you try to explicitly cast a string to Boolean, the casting appears to fail silently, leaving the Boolean with its default value of false.

However, if you leave it to the runtime to convert, the conversion works fine.

Ya learn something new every day!

Advertisements

5 thoughts on “Casting string to boolean … A little quirky….

  1. Brian Sexton says:

    Yes, ActionScript 3’s String-to-Boolean conversion does seem odd. Here is one way you can work around it:

    bool = (someVal == 'true') ? true : false;
    

    It’s a bit longer than “as Boolean”, but it should do the trick. You might want to use “===” as your comparison operator depending upon the situation.

  2. realgt says:

    wow….just ran into this myself…. i thought ActionScript was a real language until i realized it couldn’t consistenly convert the string “true” to a boolean.

    ????

  3. DenisPat says:

    I think your post is misleading. Unless things change with player version, here are the results I come up with:

    var booleanTest:Boolean = false;
    booleanTest = “true”;
    trace(“1: “+booleanTest); //1: true

    booleanTest = “false”;
    trace(“2: “+booleanTest); //2: true

    booleanTest = Boolean(“true”);
    trace(“3: “+booleanTest); //3: true

    booleanTest = Boolean(“false”);
    trace(“4: “+booleanTest); //4: true

    var stringTrue:String = “false”;
    booleanTest = stringTrue;
    trace(“5: “+booleanTest); //5: true

    stringTrue = “true”;
    booleanTest = stringTrue;
    trace(“6: “+booleanTest); //6: true

    stringTrue = “false”;
    booleanTest = stringTrue as Boolean;
    trace(“7: “+booleanTest); //7: false

    stringTrue = “true”;
    booleanTest = stringTrue as Boolean;
    trace(“8: “+booleanTest); //8: false

  4. john says:

    just to note brian you don’t need the turnary operator. this has the same effect.

    bool = (someVal == ‘true’);

  5. Antoine Lassauzay says:

    For the sake of preciseness, and because this post is highly ranked, this is totally expected, and isn’t weird at all. JavaScript works the same way.

    There’s a difference between casting and converting.
    Casting a String to a boolean fails “true” as Boolean and thus returns ‘null’, because a String and a Boolean are different thing. Converting ‘null’ to a Boolean then yields ‘false’.

    Similarly converting a non-empty string to Boolean always yields true. It’s the role of the Boolean constructor though which really comes to a simple truth-test `var bool:Boolean = input ? true : false` – this behaviour is important.

    The comparison technique works because using `==` always return true or false.

    I wish there was a simple, native way to convert 1, ‘true’, ‘TRUE’ to a boolean unfortunately it takes a lot of marshalling depending on your input.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: