JavaScript Boolean

The JavaScript Boolean object is a wrapper class and a member of global objects. It is used to convert the non-boolean values into boolean values.

Boolean object has following two values:

JavaScript Boolean Object Properties

The table given below describes properties of the Boolean object in JavaScript.

Property Description
constructor returns the function which has created the prototype of the Boolean object
prototype allows you to add properties and methods to an object

JavaScript Boolean Object Methods

Here is the table describes the methods of the Boolean object in JavaScript.

Method Description
toString() converts the boolean value into string and returns the string
valueOf() returns the primitive value of a Boolean object

Here is the general form to create a boolean object in JavaScript:

var val = new Boolean(value);

JavaScript Boolean() Function

The JavaScript Boolean() function is used to find out if an expression or variable is true or not. Here is an example:

Boolean(20 > 9)        // returns true

The above expression will return true.

JavaScript Boolean Object Example

Here is an example uses boolean object in JavaScript:

<!DOCTYPE html>
<html>
<head>
   <title>JavaScript Boolean Object Example</title>
   <script>
   function boolean_fun1()
   {
      document.getElementById("boolean_para1").innerHTML = Boolean(10 > 9);
   }
   </script>
</head>
<body>

<p>To get the answer, just click on the <b>Answer</b> button given below.</p>
<p id="boolean_para1">Is 200 > 9 ?</p>
<button onclick="boolean_fun1()">Answer</button>

</body>
</html>

Here is the output produced by the above JavaScript Boolean object example program. This is the initial output:

javascript boolean object

Now to find the answer, that is whether 200 is greater than 9 or not, just click on the Answer button. After clicking on the Answer button you will watch the following output:

javascript boolean object example

Here is the live demo output produced by the above Boolean object example program in JavaScript.

To get the answer, just click on the Answer button given below.

Is 200 > 9 ?

Here is another example of Boolean Object in JavaScript.

<!DOCTYPE HTML>
<HEAD>
   <TITLE>JavaScript Boolean Object Example</TITLE>
   <SCRIPT type="text/javascript">
   var boolean_object1=new Boolean(0);
   var boolean_object2=new Boolean(8);
   var boolean_object3=new Boolean(1);
   var boolean_object4=new Boolean("");
   var boolean_object5=new Boolean("fresherearth");
   var boolean_object6=new Boolean('False');
   var boolean_object7=new Boolean(null);
   var boolean_object8=new Boolean(NaN);

   document.write("The value 0 is boolean "+boolean_object1+"<br>");
   document.write("The value 8 is boolean "+boolean_object2+"<br>");
   document.write("The value 1 is boolean "+boolean_object3+"<br>");
   document.write("An empty string is boolean "+boolean_object4+"<br>");
   document.write("The String \"fresherearth\" is boolean "+boolean_object5+"<br>");
   document.write("The String 'False' is boolean "+boolean_object6+"<br>");
   document.write("null is boolean "+boolean_object7+"<br>");
   document.write("NaN is boolean "+boolean_object8);
   </SCRIPT>
</HEAD>
<BODY>

</BODY>
</HTML>

Here is the output produced by the above JavaScript Boolean Object example.

javascript boolean

JavaScript Online Test


« Previous Tutorial Next Tutorial »