Table of contents
Summary
The Boolean object is an object wrapper for a boolean value.
Syntax
new Boolean(value)
Parameters
value- The initial value of the
Booleanobject.
Description
The value passed as the first parameter is converted to a boolean value, if necessary. If value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object or the string "false", create an object with an initial value of true.
Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.
Any object whose value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true:
x = new Boolean(false);
if (x) {
// . . . this code is executed
}
This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to false:
x = false;
if (x) {
// . . . this code is not executed
}
Do not use a Boolean object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task:
x = Boolean(expression); // preferred x = new Boolean(expression); // don't use
If you specify any object, including a Boolean object whose value is false, as the initial value of a Boolean object, the new Boolean object has a value of true.
myFalse = new Boolean(false); // initial value of false
g = new Boolean(myFalse); // initial value of true
myString = new String("Hello"); // string object
s = new Boolean(myString); // initial value of true
Do not use a Boolean object in place of a Boolean primitive.
Properties
For properties available on Boolean instances, see Properties of Boolean instances.
prototype: Defines a property that is shared by all Boolean objects.
Methods
For methods available on Boolean instances, see Methods of Boolean instances.
The global Boolean object contains no methods of its own, however, it does inherit some methods through the prototype chain.
Boolean instances
All Boolean instances inherit from Boolean.prototype. As with all constructors, the prototype object dictates instances' inherited properties and methods.
Properties
constructor- Returns the function that created an instance's prototype. This is the
Booleanfunction by default.
Methods
- toSource
- Returns a string containing the source of the
Booleanobject; you can use this string to create an equivalent object. Overrides theObject.prototype.toSourcemethod.
toString- Returns a string of either "true" or "false" depending upon the value of the object. Overrides the
Object.prototype.toStringmethod.
valueOf- Returns the primitive value of the
Booleanobject. Overrides theObject.prototype.valueOfmethod.
Object:__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, toLocaleString, unwatch, watch
Examples
Creating Boolean objects with an initial value of false
var bNoParam = new Boolean();
var bZero = new Boolean(0);
var bNull = new Boolean(null);
var bEmptyString = new Boolean("");
var bfalse = new Boolean(false);
Creating Boolean objects with an initial value of true
var btrue = new Boolean(true);
var btrueString = new Boolean("true");
var bfalseString = new Boolean("false");
var bSuLin = new Boolean("Su Lin");
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| toSource() | -- | (Yes) | -- | -- | -- |
| Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| toSource() | -- | (Yes) | -- | -- | -- |
Mozilla Developer Network