Boolean
constructor
JS Home <<
JS Reference <<
Boolean
In JavaScript you create a boolean instance using the constructor Boolean
.
Description
Declares a boolean wrapper for holding boolean values.
The Boolean
constructor is a wrapper for a boolean value and should not be confused with the true
and false
values of the boolean primitive.
There are a some things to remember when creating Boolean
objects and how the parameter passed to the Boolean
constructor are converted to a boolean value:
- If value is omitted or is 0, -0,
null
,false
,NaN
,undefined
, or the empty string (''), the object has an initial value offalse
. - Any other values, including objects and the string 'false' create an object with an initial value of
true
. - Any values other than
undefined
andnull
evaluate to true when passed to a conditional statement.
Syntax
Signature | Description |
---|---|
aBoolean = new Boolean(value); | Declares a boolean wrapper for holding boolean values. |
Parameters
Parameter | Description |
---|---|
value | An initial value for the Boolean. |
Class Properties
Class Property | Description |
---|---|
prototype | Enables property assignment to objects of type Boolean . |
Instance Properties
Instance Property | Description |
---|---|
constructor | Returns a reference to the Boolean function that created the prototype. |
Class Methods
None.
Instance Methods
None.
Examples
The code below highlights the difference between the Boolean
constructor and the boolean primitives true
and false
.
// Create a Boolean object.
aBoolean = new Boolean(false);
if (aBoolean) {
alert('set false, but evaluates true'); // This code is executed
} else {
alert('set false'); // This code is NOT executed
}
// Set a boolean primitive.
bBoolean = false;
if (bBoolean) {
alert('set false, but evaluates true'); // This code is NOT executed
} else {
alert('set false'); // This code is executed
}
Related Tutorials
JavaScript Basic Tutorials - Lesson 9 - Booleans