The Boolean
object is an object wrapper for a boolean value.
Syntax
new Boolean([value])
Parameters
value
- Optional. The initial value of the
Boolean
object.
Description
The value passed as the first parameter is converted to a boolean value, if necessary. If value is omitted or is 0
, -0
, {{jsxref("null")}}, false
, {{jsxref("NaN")}}, {{jsxref("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 {{jsxref("undefined")}} or {{jsxref("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 {{jsxref("Statements/if...else", "if")}} statement evaluates to true
:
var 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 {{jsxref("Statements/if...else", "if")}} statement evaluates to false
:
var 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:
var x = Boolean(expression); // preferred var 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
.
var myFalse = new Boolean(false); // initial value of false var g = new Boolean(myFalse); // initial value of true var myString = new String('Hello'); // string object var s = new Boolean(myString); // initial value of true
Do not use a Boolean
object in place of a Boolean
primitive.
Properties
Boolean.length
- Length property whose value is 1.
- {{jsxref("Boolean.prototype")}}
- Represents the prototype for the
Boolean
constructor.
Methods
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 {{jsxref("Boolean.prototype")}}. As with all constructors, the prototype object dictates instances' inherited properties and methods.
Properties
Methods
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'); var bArrayProto = new Boolean([]); var bObjProto = new Boolean({});
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('ES1')}} | {{Spec2('ES1')}} | Initial definition. Implemented in JavaScript 1.0. |
{{SpecName('ES5.1', '#sec-15.6', 'Boolean')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-boolean-objects', 'Boolean')}} | {{Spec2('ES6')}} |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatIE("6.0")}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
See also
- {{jsxref("Boolean.prototype")}}
- {{Glossary("Boolean")}}
- Boolean data type (Wikipedia)