1 Reply Latest reply on Oct 31, 2007 3:41 AM by atta707

    Type conversion with Boolean()

    pallewatta
      Hey,
      I have to convert an string value which i fetch from the database to a boolean type. I tried to use Boolean(). But it returns 'true' even the string value is either "true" or "false".

      Thanks
        • 1. Re: Type conversion with Boolean()
          atta707 Level 2
          In the Flex Builder help search on "type conversion" and read about casting into boolean.

          Casting to Boolean

          Casting to Boolean from any of the numeric data types (uint, int, and Number) results in false if the numeric value is 0, and true otherwise. For the Number data type, the value NaN also results in false. The following example shows the results of casting the numbers -1, 0, and 1:

          var myNum:Number;
          for (myNum = -1; myNum<2; myNum++)
          {
          trace("Boolean(" + myNum +") is " + Boolean(myNum));
          }


          The output from the example shows that, of the three numbers, only 0 returns a value of false:

          Boolean(-1) is true
          Boolean(0) is false
          Boolean(1) is true


          Casting to Boolean from a String value returns false if the string is either null or an empty string (""). Otherwise, it returns true.

          var str1:String; // Uninitialized string is null.
          trace(Boolean(str1));// false

          var str2:String = "";// empty string
          trace(Boolean(str2));// false

          var str3:String = " "; // white space only
          trace(Boolean(str3));// true


          Casting to Boolean from an instance of the Object class returns false if the instance is null; otherwise, it returns true:

          var myObj:Object;// Uninitialized object is null.
          trace(Boolean(myObj)); // false

          myObj = new Object();// instantiate
          trace(Boolean(myObj)); // true


          Boolean variables get special treatment in strict mode in that you can assign values of any data type to a Boolean variable without casting. Implicit coercion from all data types to the Boolean data type occurs even in strict mode. In other words, unlike almost all other data types, casting to Boolean is not necessary to avoid strict mode errors. The following examples all compile in strict mode and behave as expected at run time:

          var myObj:Object = new Object();// instantiate
          var bool:Boolean = myObj;
          trace(bool); // true
          bool = "random string";
          trace(bool); // true
          bool = new Array();
          trace(bool); // true
          bool = NaN;
          trace(bool); // false


          The following table summarizes the results of casting to the Boolean data type from other data types:

          Data type or value
          Result of conversion to Boolean

          String
          false if the value is null or the empty string (""); true otherwise.

          null false
          Number, int or uint
          false if the value is NaN or 0; true otherwise.

          Object
          false if the instance is null; true otherwise.