Casting to Boolean value : Type Conversion « Number Data Type « JavaScript Tutorial






The Boolean() type cast returns true when

the value is a string with at least one character,

a number other than 0, or

an object;

The Boolean() type cast returns false when

  1. the value is an empty string,
  2. the number 0,
  3. undefined, or
  4. null.
var b1 = Boolean("");              //false -- empty string
var b2 = Boolean("JavaScript");    //true -- non-empty string
var b3 = Boolean(100);             //true -- non-zero number
var b4 = Boolean(null);            //false -- null
var b5 = Boolean(0);               //false -- zero
var b6 = Boolean(new Object());    //true -- object








5.8.Type Conversion
5.8.1.Type Conversion
5.8.2.Type-conversion adheres to the following rules
5.8.3.Converting to a String
5.8.4.Using Number's toString() method in radix mode
5.8.5.Converting to a Number
5.8.6.parseInt() method
5.8.7.parseInt() in radix mode
5.8.8.If decimal numbers contain a leading zero, it's always best to specify the radix as 10 so that you won't accidentally end up with an octal value.
5.8.9.parseFloat() method
5.8.10.Type Casting
5.8.11.Casting to Boolean value
5.8.12.Casting to Number
5.8.13.Casting type to string
5.8.14.parseInt("33.00")
5.8.15.parseFloat("1.23e-2")
5.8.16.parseFloat("1.45inch")