Boolean Objects |
Active Web |
Booleans are either ‘true’ or ‘false’. They are assigned using the words ‘true’ and ‘false’. If a Boolean is concatenated with a string then it is converted into a string of either ‘true’ or ‘false’. If a boolean uses one of the string functions then it is converted into a string.
Boolean objects are full script objects. Boolean objects are created in two ways. The first way is by using the Boolean constructor (see below). The second way is by creating a boolean primitive and then giving it a property, as this automatically converts the primitive to an object.
The boolean literals are the keywords 'true' and 'false'. You use a boolean literal to create a boolean primitive.
var myBoolean = true;
A new boolean primitive is created that has the value true.
The boolean value of an object is derived as shown in the table below:
Object Type |
Boolean Value |
Undefined |
false |
Null |
false |
Boolean primitive |
The value equals the input boolean (no conversion). |
Number primitive |
The value is false if the number is +0, -0, or NaN; otherwise the value is true. |
String primitive |
The value is false if the string is the empty string (its length is zero); otherwise the value is true. |
Object |
true (unless it is a Boolean object in which case the value equals the input boolean). |
Boolean objects are of class Boolean.
Property |
Description |
prototype |
A reference to the Boolean Prototype. This property does not enumerate and can not be deleted but can be changed. |
The Boolean object has no predefined functions.
The Boolean constructor is used to create new boolean objects. The constructor can have a single argument.
var myBoolean = new Boolean(arg);
A new boolean object is created using the argument. If the argument is not a boolean literal then the new boolean will be the boolean value of the argument.
When a boolean object is created it is given the String and Number prototypes as well as the Boolean prototype. This allows you to treat the boolean as if it were a string or a number. Shown below is the Boolean object prototype chain.
When the Boolean constructor is run as a function without the new operator it returns a boolean primitive that is the boolean resulting from converting the argument object to a boolean.
var myBoolean = Boolean(arg);
A new boolean primitive is created using the argument. If the argument is not a boolean literal then the new boolean will be the boolean value (see above) of the argument.
Constructor properties are read only (they can not be changed or deleted) and it is not possible to add new properties to the Constructor.
Property |
Description |
prototype |
A reference to the Boolean Prototype. This property does not enumerate and can not be changed or deleted. |
The Boolean constructor has no predefined functions.
This prototype can be used by any object that can be converted to a boolean value.
Property |
Description |
prototype |
A reference to the String Prototype. This property does not enumerate and can not be deleted but can be changed. |
The this reference used here always refers to the object that was used to run the function.
Function |
Description |
toString() |
If this boolean value is true, then the string "true" is returned. Otherwise, this boolean value must be false, and the string "false" is returned. This function is not generic. If the this object is not a boolean object then it returns a string of the form "[object className]" where className is the class of this object. |
valueOf() |
Returns a boolean primitive derived from the boolean value of this object. This function is not generic. If the this object is not a boolean object then this object is returned. |
Topic ID: 150038