JavaScript Programming

Boolean
Check if the Boolean object is true or false.


Boolean Object

The Boolean object is an object wrapper for a Boolean value and it is used to convert a non-Boolean value to a Boolean value, either true or false.

If the Boolean object has no initial value or if it is 0, null, "", false, or NaN, the initial value is false. Otherwise it is true (even with the string "false").

All the following lines of code create Boolean objects with an initial value of false:

var b1=new Boolean()
var b2=new Boolean(0)
var b3=new Boolean(null)
var b4=new Boolean("")
var b5=new Boolean(false)
var b6=new Boolean(NaN)

All the following lines of code create Boolean objects with an initial value of true:

var b1=new Boolean(true)
var b2=new Boolean("true")
var b3=new Boolean("false")
var b4=new Boolean("Richard")

The Boolean object's properties and methods are described below:

NN: Netscape, IE: Internet Explorer

Properties

Syntax: object.property_name

Property Description NN IE 
constructor Contains the function that created an object's prototype 4 4
prototype Allows addition of properties and methods to the object 3 4

Methods

Syntax: object.method_name()

Method Description NN IE
toString() Converts a Boolean value to a string. This method is called by JavaScript automatically whenever a Boolean object is used in a situation requiring a string 4 4
valueOf() Returns a primitive value ("true" or "false") for the Boolean object 4 4