The boolean object is used to represent the boolean value that is true or false. If the value is 0, -0, null, false, “”, undefined, NaN then it is considered as false value.
Syntax for Boolean Object
Following is the syntax to define Boolean variables using Javascript.
var a= new boolean (value)
Boolean Properties
Properties | Description |
constructor | It is used to provide a reference to Boolean function of the particular object. |
prototype | It is used to add properties and methods. |
Boolean Methods
Methods | Description |
toString( ) | It specifies Boolean object(true or false) using string. |
toSource( ) | It is used to return the string using source code of the Boolean object. |
valueOf( ) | It is used to return the value of boolean object of the given parameter |
Example Using Boolean Methods
<html> <body> <script type="text/javascript"> var a = new Boolean(1); document.write( "The value is : " + a.valueOf() ); document.write( "<br>The string is : " + a.toString() ); </script> </body> </html>
- In the above program we have used boolean methods.
- We have used two methods in the program i.e. valueOf , toString.
- We have declared a boolean variable “var = new Boolean( 1);” where 1 is equal to true.
- document.write( “The value is : ” + a.valueOf() ); is used to return the value of the boolean object .
- document.write( “The string is : ” + a.toString() ); is used to specify true or false using string.
Javascript Boolean Object Demo
- Save the file as boolean_method.html in your system.
- Just open the file in the browser, you will see the below picture in the browser.