Home > DeveloperSection > Blogs > Boolean Object in JavaScript

Boolean Object in JavaScript

JavaScript JavaScript 
0 Comment(s)
 1707  View(s)
Rate this:

Boolean Object in JavaScript


Boolean objects represent either of the two values, true or false. The Boolean object is used to convert non-Boolean value to a Boolean value (i.e. true of false).

The Boolean Object Properties are:

1.       Constructor: It returns the name of the function that created the object prototype.

2.       Prototype: It allows us to add or remove properties and method

The Boolean Object Methods are :

1.       toString() : Returns a String representation of a Boolean Object.

2.       valueOf(): Returns a primitive value of a Boolean Object.

3.       toSource(): Returns a string containing the source of the object we can use this string to create an equivalent object.

How to declare Boolean object:

var myBooleanVal = new Boolean();

        If the Boolean object has no value or the value like

·                     0

·                     -0,

·                     False,

·                     NaN,

·                     undefined,

·                     null

·                     “”

then the object is set to false.

The below example clearly explains the above points:


<html xmlns="http://www.w3.org/1999/xhtml">


    <script type="text/javascript">

        var boolean1 = new Boolean(1);

        var boolean2 = new Boolean(0);

        var boolean3 = new Boolean("");

        var boolean4 = new Boolean("false");

        var boolean5 = new Boolean(null);

        var boolean6 = new Boolean(NaN);

        document.write("1 is Boolean: " + boolean1 + "<br />");

        document.write("0 is Boolean: " + boolean2 + "<br />");

        document.write("Empty string is boolean: " + boolean3 + " <br />");

        document.write("String False is Boolean: " + boolean4 + " <br />");

        document.write("Null is Boolean: " + boolean5 + " <br />");

        document.write("NaN is Boolean: " + boolean6 + " <br />");






 Output of the above program is:



Through this blog we came to know about the Boolean object and how we can use it in our Java Script Program. Because these Boolean objects are very important part in programming language so we must know how to use it.



Don't want to miss updates? Please click the below button!

Follow MindStick