The Boolean type may have two literal values, true or false.

These values are different from numerical values 1 and 0.

In JavaScript other type of values may be converted to Boolean by using Boolean() function, like this:

var text = "JS Tutorial";

var textBoolean = Boolean(text);

The conversion of a different data type into its Boolean equivalent, if not specified as above, will be done automatically when needed, as shown here:

var text = "JS Tutorial";

if(text) alert(text);

In the above example the alert box will be shown because the variable "text" is automatically converted to true in the if statement. The conversion is done according to the following table guidelines:

Data types and their Boolean conversion

Data type Converted to True Converted to False
Boolean true false
String any non-empty string " " (empty string)
Number any non-zero number (including "infinity") 0, NaN (not-a-number)
Object any object null
Undefined n/a undefined

 

 

 

 

 

Example

The example of boolean data type:

 

›› go to examples ››