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 of false.
  • Any other values, including objects and the string 'false' create an object with an initial value of true.
  • Any values other than undefined and null 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
valueAn initial value for the Boolean.

Class Properties

Class Property Description
prototypeEnables property assignment to objects of type Boolean.

Instance Properties

Instance Property Description
constructorReturns 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
}

Press the button below to action the above code:

Related Tutorials

JavaScript Basic Tutorials - Lesson 9 - Booleans

JavaScript Basics

JavaScript Basics

JavaScript Intermediate

JavaScript Intermediate

JavaScript Advanced

JavaScript Advanced

JavaScript Reference

JavaScript Entities

Globals

Boolean

Class Properties
prototype
Instance Properties
constructor
Class Methods
None.
Instance Methods Getters
None.
Instance Methods Setters
None.

Statements

Operators