U leest de Engelstalige versie van deze inhoud, omdat er nog geen vertaling voor deze taal beschikbaar is. Help ons dit artikel te vertalen!
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 the 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, an empty array ([]
), 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 of which the 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
:
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 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. To perform this task, instead, use Boolean
as a function, or a double NOT operator:
var x = Boolean(expression); // use this... var x = !!(expression); // ...or this var x = new Boolean(expression); // don't use this!
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 = Boolean(myFalse); // initial value of true var myString = new String('Hello'); // string object var s = Boolean(myString); // initial value of true
Do not use a Boolean
object in place of a Boolean
primitive.
Note: When the non-standard property document.all
is used as an argument for this constructor, the result is a Boolean
object with the value false
. This property is legacy and non-standard and should not be used.
Properties
Boolean.length
- Length property whose value is 1.
Boolean.prototype
- Represents the prototype for the
Boolean
constructor.
Methods
While the global Boolean
object contains no methods of its own, 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
Boolean.prototype.constructor
- Returns the function that created an instance's prototype. This is the
Boolean
function by default.
Methods
Boolean.prototype.toSource()
- Returns a string containing the source of the
Boolean
object; you can use this string to create an equivalent object. Overrides theObject.prototype.toSource()
method. Boolean.prototype.toString()
- Returns a string of either
"true"
or"false"
depending upon the value of the object. Overrides theObject.prototype.toString()
method. Boolean.prototype.valueOf()
- Returns the primitive value of the
Boolean
object. Overrides theObject.prototype.valueOf()
method.
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 |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262) The definition of 'Boolean' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Boolean' in that specification. |
Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Boolean' in that specification. |
Draft |
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Boolean | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
prototype | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
toSource | Chrome No support No | Edge No support No | Firefox Full support 1 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 4 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
toString | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
valueOf | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.