JavaScript Booleans

A JavaScript Boolean represents one of two values: true or false. Booleans are fundamental to programming logic, enabling conditional execution, decision-making, and control flow. They represent the binary nature of logical conditions.

Boolean Values

Booleans can only have two values: the keywords true or false (both lowercase). These are the only two primitive boolean values in JavaScript. Booleans represent logical states: yes/no, on/off, enabled/disabled, valid/invalid. Every conditional expression in JavaScript ultimately evaluates to either true or false.

Booleans are primarily used for conditional logic in if statements, while loops, for loops, ternary operators, and logical operations. They control program flow—which code executes and which doesn't. Without booleans, programs couldn't make decisions or respond to different conditions. Boolean variables often have names starting with "is", "has", or "can" to indicate their true/false nature.

Comparison operators (==, ===, !=, !==, <, >, <=, >=) return boolean values. For example, 10 > 5 evaluates to true, while 5 > 10 evaluates to false. These comparisons are the most common way booleans are generated in code. The result of a comparison can be stored in a variable, used directly in an if statement, or combined with logical operators.

The Boolean() function (or !!double negation operator) converts any value to its boolean equivalent. Every JavaScript value has an inherent "truthiness" or "falsiness". Boolean() makes this explicit: Boolean(1) returns true, Boolean(0) returns false. This conversion happens automatically in conditional contexts, but explicit conversion with Boolean() can make code clearer.

There are exactly six falsy values in JavaScript—values that convert to false: false itself, 0 (and -0), "" (empty string), null, undefined, and NaN. Everything else is truthy and converts to true. This includes: all non-zero numbers, all non-empty strings (even "false" and "0"), all objects (including empty objects {} and arrays []), and all functions. Understanding truthy/falsy is essential for writing correct conditionals.

// Boolean values
let isActive = true;
let isComplete = false;

// Comparison returns boolean
console.log(10 > 5);  // true
console.log(5 > 10);  // false

// Boolean function
console.log(Boolean(1));        // true
console.log(Boolean(0));        // false
console.log(Boolean("Hello"));  // true
console.log(Boolean(""));       // false
console.log(Boolean(null));     // false
console.log(Boolean(undefined)); // false