You are here: Reference > JavaScript > core > objects > Boolean

Boolean object

Browser support:
Represents a boolean value as an object.

In JavaScript, the true and false keywords represent primitive boolean values. Event though the data type of these boolean primitives is boolean, they are not instances of the Boolean object.
The JavaScript interpreter does not use the Boolean object for implicit data type conversion (when a non-boolean value needs to be converted to boolean, for example in an if statement), but the Boolean object can be used as a function to convert a value to a primitive boolean value explicitly. See Example 3 for details.
When the JavaScript interpreter needs to convert a primitive boolean value to an object, it uses the Boolean object. Example 5 illustrates this mechanism.

Syntax:

Creating a new Boolean object:
var bol = new Boolean ([initState]);
If initState is not specified, the created Boolean object represents false. When initState is specified and its value is 'true' (converted to a primitive boolean type if needed), then the Boolean object represents true, in any other cases false.

Members:

The Boolean object inherits from the Boolean.prototype and Function.prototype objects. The following lists only contain the members of the Boolean and Boolean.prototype objects.

Properties:

Property Support Description
prototype
Returns a reference to the Boolean.prototype object. The Boolean.prototype object allows adding properties and methods to the Boolean object, which can be used with instances of the Boolean object, like any predefined property or method. The prototype property is static, it cannot be accessed from an instance of the Boolean object, only Boolean.prototype is allowed.

(*) - The property is inherited from the Boolean.prototype.

Methods:

Method Support Description
toSource ( )*
Returns a string representing the source code of the current Boolean object.
toString ( )*
Returns a string representing the value of the current Boolean object.
When a boolean needs to be converted to a string, the JavaScript interpreter automatically calls its toString method.
valueOf ( )*
Returns the primitive value of the Boolean object.

(*) - The method is inherited from the Boolean.prototype.

Examples:

Example 1:

Creating Boolean objects with an initial value of false:
new Boolean (false);
new Boolean ();
new Boolean ("");
new Boolean (0);
new Boolean (null);
Did you find this example helpful? yes no

Example 2:

Creating Boolean objects with an initial value of true:
new Boolean (true);
new Boolean ("true");
new Boolean ("false");
new Boolean ("high");
new Boolean (3);
new Boolean (-123);
Did you find this example helpful? yes no

Example 3:

Converts a value to a primitive boolean value explicitly with the Boolean function:
x = Boolean (2);    // x is true
x = Boolean ("");   // x is false
x = Boolean ("false");  // x is true
x = Boolean (null); // x is false
Did you find this example helpful? yes no

Example 4:

A Boolean object always evaluates to true in a condition regardless of whether it represents true or false value.
var x = Boolean (false);
if (x) {
    alert ("The x variable evaluates to true.");
}
else {
    alert ("The x variable evaluates to false.");
}
Did you find this example helpful? yes no

Example 5:

Adding a new method to the Boolean object and calling it for a primitive boolean value:
Boolean.prototype.getDetails = function () { 
    this.valueOf () ? alert ("The value is true!") : alert ("The value is false!"); 
};

    // false and true are primitive boolean values, not instances of the Boolean object,
    // but they will be converted to Boolean objects before the getDetails method is called
false.getDetails ();
true.getDetails ();

    // representing the same with an instance of the Boolean object
var bool = new Boolean (false);
    // bool is an instance of the Boolean object
    // no conversion is needed
bool.getDetails ();

var bool = new Boolean (true);
bool.getDetails ();
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content