Skip to content

JavaScript Booleans

A JavaScript Boolean represents one of two values: true or false. Booleans are fundamental for controlling program flow and making logical comparisons.

In programming, you often need a data type that can represent a binary state, such as:

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

JavaScript provides the Boolean data type for this purpose. It can only hold the values true or false.

let isJavaScriptFun = true;
let isGameOver = false;

You can use the Boolean() function to explicitly convert any value to its boolean equivalent (to find out if an expression or variable is truthy or falsy).

console.log(Boolean(10 > 9)); // returns true
console.log(Boolean(0)); // returns false
console.log(Boolean("hello")); // returns true

However, explicit conversion with Boolean() is often unnecessary, as JavaScript performs implicit coercion in boolean contexts (like if statements).

if (10 > 9) { // 10 > 9 evaluates to true
console.log("This will run.");
}

Booleans are the cornerstone of comparison operations and conditional statements.

Refer to chapters on JavaScript Comparisons and Conditional Statements for a full overview. Here are some examples:

OperatorDescriptionExample
==Equal to (loose equality, performs type coercion)if (value == 10) { /* … */ }
===Strictly equal to (no type coercion)if (value === 10) { /* … */ }
!=Not equal to (loose inequality)if (status != “active”) { /* … */ }
!==Strictly not equal toif (status !== “active”) { /* … */ }
>Greater thanif (score > 9000) { /* … */ }
<Less thanif (age < 18) { /* … */ }
>=Greater than or equal toif (quantity >= 1) { /* … */ }
<=Less than or equal toif (price <= 50) { /* … */ }
The boolean result of an expression (whether it’s truthy or falsy) determines the behavior of conditional logic in JavaScript.

In JavaScript, a value is considered “truthy” if it evaluates to true in a boolean context. Most values are truthy, including:

  • The boolean true itself.
  • Any non-empty string (e.g., "hello", "false").
  • Any non-zero number (e.g., 100, -15, 3.14).
  • Any non-zero BigInt (e.g. 1n).
  • All objects (including arrays and functions).
// Examples of truthy values
console.log(Boolean(100)); // true
console.log(Boolean(3.14)); // true
console.log(Boolean(-15)); // true
console.log(Boolean("Hello")); // true
console.log(Boolean("false")); // true (it's a non-empty string)
console.log(Boolean({})); // true (empty object)
console.log(Boolean([])); // true (empty array)
console.log(Boolean(function(){})); // true (function)

A value is “falsy” if it evaluates to false in a boolean context. There are only a few falsy values in JavaScript:

  • The boolean false itself.
  • The number 0 (zero).
  • The number -0 (negative zero).
  • The BigInt 0n (zero BigInt).
  • The empty string "" (or '' or ).
  • null.
  • undefined.
  • NaN (Not a Number).
// Examples of falsy values
console.log(Boolean(false)); // false
console.log(Boolean(0)); // false
console.log(Boolean(-0)); // false
console.log(Boolean(0n)); // false
console.log(Boolean("")); // false
console.log(Boolean(null)); // false
console.log(Boolean(undefined)); // false
console.log(Boolean(NaN)); // false

Understanding truthy and falsy values is essential for writing concise and correct conditional logic.

Similar to numbers and strings, booleans can be created as objects using new Boolean(). This is generally not recommended.

const primitiveTrue = true;
const objectTrue = new Boolean(true);
console.log(typeof primitiveTrue); // "boolean"
console.log(typeof objectTrue); // "object"

A Boolean object, even if its value is false (e.g., new Boolean(false)), is itself an object and therefore truthy. This can lead to very confusing behavior in conditional statements.

if (new Boolean(false)) {
console.log("This runs because new Boolean(false) is an object, which is truthy!");
}
Always use primitive booleans (true and false) directly. Avoid new Boolean().

Boolean Properties and Methods (via Wrapper Objects)

Section titled “Boolean Properties and Methods (via Wrapper Objects)”

Primitive boolean values (true, false) do not have properties or methods. However, JavaScript temporarily wraps them in a Boolean object when you try to access a property or method on them. This allows them to use methods inherited from Boolean.prototype.

The main methods are:

  • valueOf(): Returns the primitive value of the Boolean object.
  • toString(): Returns a string representing the boolean value ("true" or "false").
const myBool = true;
console.log(myBool.toString()); // "true"
console.log(myBool.valueOf()); // true

In practice, you rarely need to call these methods directly on boolean primitives.

For a comprehensive understanding of booleans, truthy/falsy values, and related operators, consult the MDN Web Docs for JavaScript Booleans and Equality comparisons.