How to convert String to Boolean in ActionScript?

I have the following code:

var bool:String = "true";

Without an if or switch statement, how can this be converted to a boolean object?

+5
source share
1 answer

You can use:

var boolString:String = "true";
var boolValue:Boolean = boolString == "true"; // true
var boolString2:String = "false";
var boolValue2:Boolean = boolString2 == "true"; // false

Edit

The comment below suggests using

var boolValue:Boolean = (boolString == "true") ? true : false;

This just complicates the code for no reason, as the evaluation happens in part:

(boolString == "true")

Using a ternary operator is equivalent to:

var tempValue:Boolean = boolString == "true"; // returns true: this is what I suggested
var boolValue:Boolean = tempValue ? true : false; // this is redundant
+17
source

All Articles