JavaScript » Objects » Boolean

Syntax:
new Boolean(value)

The Boolean object is an object wrapper for a Boolean value and is constructed with the above Boolean constructor.

If there is no initial value or if it is 0, -0, null, false, NaN, undefined, or the empty string (""), the initial value is false. Otherwise, even with the string "false", it is true.

Examples

Code:
x = new Boolean("false")
if(x)
Explanation:

Any Boolean object that is passed to a conditional statement (except those with an initial value of null or undefined) evaluates to true. So, for instance, the conditional statement in this code evaluates to true.

Code:
x = false
if(x)
Explanation:

However, this does not apply to Boolean primitives, and the conditional statement in the following code evaluates to false.

Code:
x = Boolean(a+b)
Explanation:

In JavaScript 1.3 and later versions, don't use a Boolean object instead of a Boolean primitive, nor should you use a Boolean object to convert a non-Boolean value to a Boolean one. To do so use Boolean as a function. This example converts the expression 'a+b' to a Boolean value.

Code:
x = new Boolean()
x = new Boolean(0)
x = new Boolean(-0)
x = new Boolean(null)
x = new Boolean(false)
x = new Boolean(NaN)
x = new Boolean(undefined)
x = new Boolean("")
Explanation:

All of these objects have an initial value of false.

Code:
myBool = new Boolean(false)
x = new Boolean(myBool)
x = new Boolean("false")
Explanation:

In these examples the Boolean object 'x' has an initial value of true

Properties

constructor

Syntax: Object.constructor

This specifies a function to create an object's property and is inherited by all objects from their prototype.

prototype

Syntax: Object.prototype.name = value

This allows the addition of properties and methods to any object.

Methods

eval

Syntax: Object.eval(string)

The eval method is deprecated as a method of Object, but is still used as a high level function. It evaluates a string of JavaScript in the context of an object.

toSource

Syntax: Object.toSource()

The toSource method returns a literal representing the source code of an object. This can then be used to create a new object.

toString

Syntax: Object.toString()

The toString method returns a string representing a specified object.

unwatch

Syntax: Object.unwatch(property)

This method removes a watchpoint set for an object and property name with the watch method.

valueOf

Syntax: Object.valueOf()

This method returns a primitive value for a specified object.

watch

Syntax: Object.watch(property, handlerfunction)

This method adds a watchpoint to a property of the object.

See Also: