JavaScript Boolean Object

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

constructorIt is used to provide a reference to Boolean function of the particular object.
prototypeIt is used to add properties and methods.

Boolean Methods

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

<script type="text/javascript">

	var a = new Boolean(1);
	document.write( "The value is : " + a.valueOf() );
	document.write( "<br>The string is : " + a.toString() );

  • 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.

When the execution process is completed successfully we will get the following output:

Javascript Boolean Object

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!