JavaScript Boolean Object
In this JavaScript tutorial you will learn about JavaScript boolean object, how to pass value to the boolean object, properties and methods used with boolean object.
Usage of the Boolean Object:
The Boolean object is a wrapper for the Boolean data type. The Boolean object is used to convert a non-Boolean value to a Boolean value. If a programmer wants to convert a non-Boolean value to a Boolean value, he can use Boolean as a function to perform this task, instead of using a Boolean object to convert a non-Boolean value to a Boolean value.
The Boolean object returns false when the passed value to the object is any one of the following:
- 0
- ""
- false
- undefined
- NaN
Other than the above values, any other value passed to the Boolean Object returns true. The programmer must clearly understand that a Boolean object is different from Boolean data type.
When an object’s value is not undefined or null (including a Boolean object whose value is false), it evaluates to true when passed to a conditional statement.
For example, the condition in the following if statement evaluates to true:
|
The above concept does not apply for Boolean primitives.
For example, the condition in the following if statement evaluates to false:
|
General Syntax for defining the Boolean Object:
General Syntax for defining the Boolean Object is as follows:
|
Here the keyword new is used to define the Boolean object.
For example:
|
The above example defines a Boolean Object exforsys.
How to pass value to the Boolean object:
The general syntax for writing this is as follows:
|
The above statement creates a Boolean Object given in a variable name with the initial value set to the value passed in arguments of the Boolean. The value can be any one of the following:
0, "", false, undefined, NaN, true, any string and so on.
For example:
|
The above example creates two Boolean objects exforsys and sample with initial value as true.
|
The above example creates two Boolean objects exforsys1 and sample1 with initial value as false.
 }
Property of Boolean Object:
Properties are the values associated with an object. A Boolean object has only one property, the value of the object – which is either true or false.
Methods used with Boolean Object:
Two methods used with Boolean Objects are:
valueOf()
toString()
valueOf():
valueOf() is used to return the primitive value of a Boolean object.
General Syntax of the valueOf method:
|
For example:
|
In the above example, a boolean object exforsys is created with its initial value set as true. The value of the boolean object exforsys is printed using the valueOf() with the boolean object exforsys resulting in a true value.
toString():
The toString() method is used to convert a Boolean value to a string and returns the result.
General Syntax of the toString() method:
|
For example:
|
The output of the above example is
|
In the above example, a boolean object exforsys is created with its initial value set as true. Then this Boolean value true is converted to a string using the method toString() along with the boolean object exforsys. This is outputs, resulting in a true string.