Boolean
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Boolean
values can be one of two values:true
orfalse
, representing the truth value of a logical proposition.
Description
Boolean values are typically produced byrelational operators,equality operators, andlogical NOT (!
). They can also be produced by functions that represent conditions, such asArray.isArray()
. Note thatbinary logical operators such as&&
and||
return the values of the operands, which may or may not be boolean values.
Boolean values are typically used in conditional testing, such as the condition forif...else
andwhile
statements, theconditional operator (? :
), or the predicate return value ofArray.prototype.filter()
.
You would rarely need to explicitly convert something to a boolean value, as JavaScript does this automatically in boolean contexts, so you can use any value as if it's a boolean, based on itstruthiness. You are also encouraged to useif (condition)
andif (!condition)
instead ofif (condition === true)
orif (condition === false)
in your own code so you can take advantage of this convention. However, making sure that values representing conditions are always booleans can help clarify the intent of your code.
// Do this:// This always returns a boolean valueconst isObject = (obj) => !!obj && typeof obj === "object";// Or this:const isObject = (obj) => Boolean(obj) && typeof obj === "object";// Or this:const isObject = (obj) => obj !== null && typeof obj === "object";// Instead of this:// This may return falsy values that are not equal to falseconst isObject = (obj) => obj && typeof obj === "object";
Boolean primitives and Boolean objects
For converting non-boolean values to boolean, useBoolean
as a function or use thedouble NOT operator. Do not use theBoolean()
constructor withnew
.
const good = Boolean(expression);const good2 = !!expression;
const bad = new Boolean(expression); // don't use this!
This is becauseall objects, including aBoolean
object whose wrapped value isfalse
, aretruthy and evaluate totrue
in places such as conditional statements. (See also theboolean coercion section below.)
if (new Boolean(true)) { console.log("This log is printed.");}if (new Boolean(false)) { console.log("This log is ALSO printed.");}const myFalse = new Boolean(false); // myFalse is a Boolean object (not the primitive value false)const g = Boolean(myFalse); // g is trueconst myString = new String("Hello"); // myString is a String objectconst s = Boolean(myString); // s is true
Warning:You should rarely find yourself usingBoolean
as a constructor.
Boolean coercion
Many built-in operations that expect booleans first coerce their arguments to booleans.The operation can be summarized as follows:
- Booleans are returned as-is.
undefined
turns intofalse
.null
turns intofalse
.0
,-0
, andNaN
turn intofalse
; other numbers turn intotrue
.0n
turns intofalse
; otherBigInts turn intotrue
.- The empty string
""
turns intofalse
; other strings turn intotrue
. - Symbols turn into
true
. - All objects become
true
.
Note:A legacy behavior makesdocument.all
returnfalse
when used as a boolean, despite it being an object. This property is legacy and non-standard and should not be used.
Note:Unlike other type conversions likestring coercion ornumber coercion, boolean coercion does not attempt toconvert objects to primitives by calling user methods.
In other words, there are only a handful of values that get coerced tofalse
— these are calledfalsy values. All other values are calledtruthy values. A value's truthiness is important when used with logical operators, conditional statements, or any boolean context.
There are two ways to achieve the same effect in JavaScript.
- Double NOT:
!!x
negatesx
twice, which convertsx
to a boolean using the same algorithm as above. - The
Boolean()
function:Boolean(x)
uses the same algorithm as above to convertx
.
Note that truthiness is not the same as beingloosely equal totrue
orfalse
.
if ([]) { console.log("[] is truthy");}if ([] == false) { console.log("[] == false");}// [] is truthy// [] == false
[]
is truthy, but it's also loosely equal tofalse
. It's truthy, because all objects are truthy. However, when comparing withfalse
, which is a primitive,[]
is also converted to a primitive, which is""
viaArray.prototype.toString()
. Comparing strings and booleans results in both beingconverted to numbers, and they both become0
, so[] == false
istrue
. In general, falsiness and== false
differ in the following cases:
NaN
,undefined
, andnull
are falsy but not loosely equal tofalse
."0"
(and other string literals that are not""
butget coerced to 0) is truthy but loosely equal tofalse
.- Objects are always truthy, but their primitive representation may be loosely equal to
false
.
Truthy values are even more unlikely to be loosely equal totrue
. All values are either truthy or falsy, but most values are loosely equal to neithertrue
norfalse
.
Constructor
Boolean()
Creates
Boolean
objects. When called as a function, it returns primitive values of type Boolean.
Instance properties
These properties are defined onBoolean.prototype
and shared by allBoolean
instances.
Boolean.prototype.constructor
The constructor function that created the instance object. For
Boolean
instances, the initial value is theBoolean
constructor.
Instance methods
Boolean.prototype.toString()
Returns a string of either
true
orfalse
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 false values
const bNoParam = Boolean();const bZero = Boolean(0);const bNull = Boolean(null);const bEmptyString = Boolean("");const bfalse = Boolean(false);
Creating true values
const btrue = Boolean(true);const btrueString = Boolean("true");const bfalseString = Boolean("false");const bSuLin = Boolean("Su Lin");const bArrayProto = Boolean([]);const bObjProto = Boolean({});
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-boolean-objects |
Browser compatibility
See also
- Boolean
- Boolean primitives
- Boolean data type on Wikipedia