The JavaScript Boolean Object

The boolean data type is a primitive data type that can hold one of two values: true or false. Boolean values are used to represent truth values or logical values.

JavaScript provides a Boolean object as a wrapper for the two boolean values. The new Boolean() constructor creates a Boolean object:

var bool1 = new Boolean([value]);

The value passed as a parameter may be of any type. It will be converted to a boolean value, if necessary.

Any value can be interpreted as a boolean value. Values such as undefined, null, false, 0, NaN and empty strings evaluate to false. Passing any other value to the new Boolean() constructor creates an object with the value of true.

The Boolean() global function evaluates the truth value of a given value:

var bool2 = Boolean(value);

As with the constructor, the argument may be of any type. The Boolean() function returns a boolean primitive, either true or false.

The Boolean Object Properties

constructor
Returns a reference to the function that created the Boolean object
prototype
Allows the addition of properties and methods to all objects

The Boolean Object Methods

toString()
Returns a string representation of the Boolean object, either "true" or "false"
valueOf()
Returns the primitive value of the Boolean object, either true or false

See also

If you see a typo, want to make a suggestion or have anything in particular you'd like to know more about, please drop us an e-mail at hello at diveintojavascript dot com.

Copyright © 2010-2013 Dive Into JavaScript