Exforsys.com
 
Home Tutorials JavaScript
 

JavaScript Boolean Object

 

JavaScript Boolean Object

Page 1 of 2

JavaScript Boolean Object

In this JavaScript tutorial you will learn about JavaScript boolean object, how to pass value to the boolean object, properties and methods used with boolean object.


Usage of the Boolean Object:

The Boolean object is a wrapper for the Boolean data type. The Boolean object is used to convert a non-Boolean value to a Boolean value. If a programmer wants to convert a non-Boolean value to a Boolean value, he can use Boolean as a function to perform this task, instead of using a Boolean object to convert a non-Boolean value to a Boolean value.


The Boolean object returns false when the passed value to the object is any one of the following:


  • 0
  • ""
  • false
  • undefined
  • NaN

Other than the above values, any other value passed to the Boolean Object returns true. The programmer must clearly understand that a Boolean object is different from Boolean data type.


When an object’s value is not undefined or null (including a Boolean object whose value is false), it evaluates to true when passed to a conditional statement.


For example, the condition in the following if statement evaluates to true:



z = new Boolean(false);
if(z)        //Condition returns true



The above concept does not apply for Boolean primitives.


For example, the condition in the following if statement evaluates to false:



y =
false;
if(y)     //Condition returns false



General Syntax for defining the Boolean Object:

General Syntax for defining the Boolean Object is as follows:



var variable_name=new Boolean()



Here the keyword new is used to define the Boolean object.


For example:



var exforsys=new Boolean()



The above example defines a Boolean Object exforsys.


How to pass value to the Boolean object:

The general syntax for writing this is as follows:



var variable_name = new Boolean(value)



The above statement creates a Boolean Object given in a variable name with the initial value set to the value passed in arguments of the Boolean. The value can be any one of the following:


0, "", false, undefined, NaN, true, any string and so on.


For example:



var exforsys = new Boolean("Training")
var sample = new Boolean(true)




The above example creates two Boolean objects exforsys and sample with initial value as true.



var exforsys1 = new Boolean(false)
var sample1 = new Boolean(NaN)



The above example creates two Boolean objects exforsys1 and sample1 with initial value as false.


 

Next Page: JavaScript Boolean Object Properties and Methods


Read Next: JavaScript Array Object



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape