Chapter III · Data Types

Boolean

The boolean data type represents logical truth entities with exactly two possible values: true and false.

01 What is a Boolean?

A boolean represents a logical entity with two literal states: true and false.

Booleans form the foundation of conditional logic, driving control flow statements like if, while, and ternary evaluations.

02 Simple Example

Using booleans to control conditional execution branching.

boolean.js
const isLoggedIn = true;
if (isLoggedIn) {
  console.log("Welcome back!");
} else {
  console.log("Please log in.");
}

Relational comparison operators (like >, ===) evaluate expressions down to boolean values.

03 How It Works

In JavaScript, non-boolean values can be evaluated in boolean contexts. Values convert implicitly to true or false based on whether they are truthy or falsy.

Variable Reference Primitive Value

Boolean primitives store lightweight binary flags representing truth states.

04 Step-by-Step

  1. A logical comparison or expression is evaluated.

  2. The engine resolves the result to either true or false.

  3. Control flow branches according to the resolved boolean state.

05 MentalModel

"A light switch that is either toggled ON (true) or OFF (false)."

It determines whether power flows through a specific execution branch in your code.

06 Interview Questions

Q: What's the difference between Boolean(value) and new Boolean(value)?

Boolean(value) coerces to a primitive true/false; new Boolean(value) creates a Boolean object wrapper, which is truthy even when wrapping false — a common gotcha, best avoided.

Key Takeaway

Booleans provide the binary true/false logic required for conditionals and decision-making in programs.

Related Concepts