docs.rodeo

MDN Web Docs mirror

Boolean

{{JSRef}} 

Boolean values can be one of two values: true or false, representing the truth value of a logical proposition.

Description

Boolean values are typically produced by relational operators, equality operators, and logical NOT (!). They can also be produced by functions that represent conditions, such as {{jsxref("Array.isArray()")}} . Note that binary 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 for {{jsxref("Statements/if...else", "if...else")}}  and {{jsxref("Statements/while", "while")}}  statements, the conditional operator (? :), or the predicate return value of {{jsxref("Array.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 its truthiness. You are also encouraged to use if (condition) and if (!condition) instead of if (condition === true) or if (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 value
const 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 false
const isObject = (obj) => obj && typeof obj === "object";

Boolean primitives and Boolean objects

For converting non-boolean values to boolean, use Boolean as a function or use the double NOT operator. Do not use the Boolean() constructor with new.

const good = Boolean(expression);
const good2 = !!expression;
const bad = new Boolean(expression); // don't use this!

This is because all objects, including a Boolean object whose wrapped value is false, are {{glossary("truthy")}}  and evaluate to true in places such as conditional statements. (See also the boolean 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 true
const myString = new String("Hello"); // myString is a String object
const s = Boolean(myString); // s is true

[!WARNING] You should rarely find yourself using Boolean 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:

[!NOTE] A legacy behavior makes document.all return false 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 like string coercion or number coercion, boolean coercion does not attempt to convert objects to primitives by calling user methods.

In other words, there are only a handful of values that get coerced to false — these are called falsy values. All other values are called truthy 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.

Note that truthiness is not the same as being loosely equal to true or false.

if ([]) {
  console.log("[] is truthy");
}
if ([] == false) {
  console.log("[] == false");
}
// [] is truthy
// [] == false

[] is truthy, but it’s also loosely equal to false. It’s truthy, because all objects are truthy. However, when comparing with false, which is a primitive, [] is also converted to a primitive, which is "" via {{jsxref("Array.prototype.toString()")}} . Comparing strings and booleans results in both being converted to numbers, and they both become 0, so [] == false is true. In general, falsiness and == false differ in the following cases:

Truthy values are even more unlikely to be loosely equal to true. All values are either truthy or falsy, but most values are loosely equal to neither true nor false.

Constructor

Instance properties

These properties are defined on Boolean.prototype and shared by all Boolean instances.

Instance methods

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

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN