Boolean Object

The Boolean object represents either a true or false value. The keyword new is used to create a Boolean object.

Syntax

var nameofVariable=new Boolean();


When you create a new boolean object with nothing inside the parentheses() then the outcome would be false.


<script>
var result=new Boolean()
document.write("result =" + result)
</script>

Displays



If we put the value 1 inside the parentheses then the outcome would be true.


<script>
var result=new Boolean(1)
document.write("result =" + result)
</script>

Displays



If we put a real number inside the parentheses then the outcome would be true.


<script>
var result1=new Boolean(-2)
var result2=new Boolean(12.7)
document.write("result1 =" + result1)
document.write("result2 =" + result2)
</script>

Displays



If we put a string value inside the parentheses then the outcome would be true, even if you have the value "false".


<script>
var result1=new Boolean("false")
var result2=new Boolean("Jane")
document.write("result1 =" + result1)
document.write("result2 =" + result2)
</script>

Displays




However, if you have the value false without the string then the outcome would be false.

<script>
var result=new Boolean(false)
document.write("result =" + result)
</script>

Displays




The following values: 0, "", null or NaN would result in false.


<script>
var result1=new Boolean(0)
var result2=new Boolean("")
var result3=new Boolean(null)
var result4=new Boolean(NaN)
document.write("result1 =" + result1)
document.write("result2 =" + result2)
document.write("result3 =" + result3)
document.write("result4 =" + result4)
</script>

Displays





Primitive and Object Boolean

The Boolean object values true and false should not be confused with the primitive boolean values. Look what happens when you use a conditional statement with the value being false.


<script>
//object
var closeDoor=new Boolean(false)
if(closeDoor)
{
document.write("The door is open")
}
else
{
document.write("The door is closed")
</script>

Displays




<script>
//primitive
var shutDoor=false;
if(shutDoor)
{
document.write("The door is open")
}
else
{
document.write("The door is closed")
</script>

Displays




Look what happens when we use the comparison operators to compare the values and type.


<script>

//primitive
var shutDoor=false;

//object
var doorClosed=new Boolean(false)

document.write("Both have the same values:")
document.write( doorClosed==shutDoor)
document.write("The values are the same but the type differs, so outcome will be:")
document.write(doorClosed===shutDoor)

</script>


Displays






Property

Property Explanation
constructor() Returns a reference to the Boolean function that created the object. (returns:function Boolean(){[native code]})
prototype The prototype allows you to add properties and methods to an object. In the section Objects you will learn more about prototype.




Method

Method Explanation
toString() The toString() converts a boolean value to a string.
valueOf() The valueOf() returns the primitive boolean value, which is contained in the Boolean object.



Now do the Exercise!