J

JavaScript Handbook

Clean • Professional

Boolean

3 minute

Boolean

A Boolean in JavaScript is a primitive data type that represents one of two possible values: true or false.

Booleans are mainly used in conditional statements, comparisons, and logical operations to control program flow.

They form the foundation of decision-making in JavaScript — for example, determining whether a condition is met, an element exists, or a comparison is valid.

  • Type: typeof true'boolean'
  • Immutable: Booleans are primitive and cannot be changed.
  • Two States: true and false (case-sensitive).
  • Control Flow: Used in if, else, while, for, and ternary (? :) expressions.
  • Comparison Result: Expressions like 5 > 3 automatically return Boolean values.

Creating Boolean Values

Using Boolean Literals

You can directly assign true or false:

let isActive = true;
let isComplete = false;

Using the Boolean() Constructor

Converts a value to a Boolean.

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

Using the new Boolean() Object (Not Recommended)

This creates a Boolean object, not a primitive:

let boolObj = new Boolean(false);
console.log(typeof boolObj);  // 'object'
console.log(boolObj);         // [Boolean: false]

Boolean Conversion Rules

JavaScript automatically converts (or coerces) other types into Boolean values in logical contexts (like if statements).

Values are considered either truthy or falsy.

Truthy and Falsy Values

Falsy Values

The following always evaluate to false:

false
0
-0
0n          // BigInt zero
""          // empty string
null
undefined
NaN

Truthy Values

Everything else is truthy — for example:

true
42
"hello"
[]
{}
function() {}
"0"
"false"

Example:

if ("") console.log("Truthy");
else console.log("Falsy"); // Output: Falsy

Boolean Operations

JavaScript provides logical operators that return Boolean values or expressions.

Logical AND (&&)

Returns true only if both operands are true.

console.log(true && true);   // true
console.log(true && false);  // false

In expressions, it returns the first falsy value or the last truthy one:

console.log("A" && "B"); // "B"
console.log("A" && "");  // ""

Logical OR (||)

Returns true if at least one operand is true.

console.log(true || false);  // true
console.log(false || false); // false

In expressions, returns the first truthy value:

console.log("" || "Default"); // "Default"

Logical NOT (!)

Inverts a Boolean value.

console.log(!true);  // false
console.log(!0);     // true

Double NOT (!!)

Converts any value into a strict Boolean.

console.log(!!"hello"); // true
console.log(!!0);       // false

Comparison and Equality with Boolean

Booleans are often the result of comparison operators:

Strict equality (===) checks both value and type,

while loose equality (==) performs type coercion.

console.log(5 > 3);   // true
console.log(5 == "5"); // true (loose equality)
console.log(5 === "5"); // false (strict equality)

Boolean and Type Conversion

To Boolean

console.log(Boolean(0));       // false
console.log(Boolean("text"));  // true

To Number

console.log(Number(true));  // 1
console.log(Number(false)); // 0

To String

console.log(String(true));  // "true"
console.log(String(false)); // "false"

From Other Types

console.log(Boolean([]));  // true
console.log(Boolean({}));  // true
console.log(Boolean(null)); // false

Boolean in Conditional Statements

Booleans control logic flow in conditional constructs:

let isLoggedIn = true;

if (isLoggedIn) {
  console.log("Welcome!");
} else {
  console.log("Please log in.");
}

Using shorthand:

isLoggedIn && console.log("Welcome again!");

Common Pitfalls with Boolean

1. Boolean Object vs Primitive

let a = false;
let b = new Boolean(false);
console.log(a == b);   // true (loose)
console.log(a === b);  // false (different types)

2. Unexpected Truthy/Falsy Conversions

console.log(Boolean("false")); // true (non-empty string)
console.log(Boolean([]));      // true (array is an object)
console.log(Boolean(null));    // false

3. Using !! for Safe Conversion

let val = "Hello";
console.log(!!val); // true

Real-World Examples

Example 1: Form Validation

let input = "";
if (!input) {
  console.log("Please enter a value."); // Triggered because input is falsy
}

Example 2: Default Values with OR Operator

let username = "" || "Guest";
console.log(username); // "Guest"

Example 3: Short-Circuit Logic

let isAdmin = true;
isAdmin && console.log("Access granted");

 

Article 0 of 0