The Boolean
object is an object wrapper for a boolean value.
The Boolean
object is an object wrapper for a boolean value.
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
:
const 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
:
const 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:
const good = Boolean(expression); // use this const good2 = !!(expression); // or this const bad = 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
.
const myFalse = new Boolean(false); // initial value of false const g = Boolean(myFalse); // initial value of true const myString = new String('Hello'); // string object const 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.
When using ==
to loosely compare an object to a boolean primitive, it's important to have a clear understanding of what's actually being compared. Consider the following example:
if ([]) { console.log("[] is truthy")} // logs "[] is truthy" if ([] == false) { console.log("[] == false")} // logs "[] == false"
The reason for [] == false
even though []
is truthy is: the comparison [] == false
compares the value of []
to false
. And to get the value of []
, the JavaScript engine first calls [].toString()
. That results in ""
, and that is what's actually compared to false
. In other words, [] == false
is equivalent to "" == false
. And ""
is falsy — and so that's what explains the behavior in the example.
Boolean()
Creates a new Boolean
object.
Boolean.prototype.toString()
Returns a string of either true
or false
depending upon the value of the object. Overrides the Object.prototype.toString()
method.
Boolean.prototype.valueOf()
Returns the primitive value of the Boolean
object. Overrides the Object.prototype.valueOf()
method.
const bNoParam = new Boolean(); const bZero = new Boolean(0); const bNull = new Boolean(null); const bEmptyString = new Boolean(''); const bfalse = new Boolean(false);
const btrue = new Boolean(true); const btrueString = new Boolean('true'); const bfalseString = new Boolean('false'); const bSuLin = new Boolean('Su Lin'); const bArrayProto = new Boolean([]); const bObjProto = new Boolean({});
Specification |
---|
ECMAScript Language Specification # sec-boolean-objects |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
Boolean |
1 |
12 |
1 |
3 |
4 |
1 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
1.0 |
0.10.0 |
Boolean |
1 |
12 |
1 |
3 |
3 |
1 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
1.0 |
0.10.0 |
toString |
1 |
12 |
1 |
3 |
4 |
1 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
1.0 |
0.10.0 |
valueOf |
1 |
12 |
1 |
4 |
4 |
1 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
1.0 |
0.10.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean