The boolean type has only two values: true and false. Booleans are essential for control flow — every condition in if, while, and loops ultimately evaluates to a boolean (or a value treated as one).

Boolean Literals

  let isActive = true;
let isDeleted = false;

console.log(typeof isActive); // 'boolean'
  

Comparison Results

Comparison operators always return booleans:

  console.log(5 > 3);        // true
console.log(5 === '5');    // false
console.log('a' === 'a');  // true
  

Truthy and Falsy Values

In JavaScript, every value is either truthy or falsy when used in a boolean context.

Falsy values (only 8)

Value Example
false if (false)
0 if (0)
-0
0n BigInt zero
"" Empty string
null
undefined
NaN

Truthy values

Everything else is truthy, including:

  console.log(Boolean('hello'));  // true
console.log(Boolean([]));       // true (empty array is truthy!)
console.log(Boolean({}));       // true
console.log(Boolean('0'));      // true (non-empty string)
console.log(Boolean('false'));  // true
  

Using Truthy/Falsy in Code

  let username = '';

// Short-circuit: use default if username is falsy
let displayName = username || 'Guest';
console.log(displayName); // 'Guest'

// Nullish coalescing: only null/undefined trigger default
let count = 0;
console.log(count || 10);  // 10 (0 is falsy)
console.log(count ?? 10);  // 0  (0 is not nullish)
  

Converting to Boolean

  Boolean(1);       // true
Boolean(0);       // false
!!'hello';        // true (double negation trick)
!!'';             // false
  

Practical Examples

  // Check if array has items
let items = [];
if (items.length) {
    console.log('Has items');
} else {
    console.log('Empty'); // runs
}

// Guard clause
function greet(name) {
    if (!name) {
        console.log('Please provide a name');
        return;
    }
    console.log('Hello, ' + name);
}

greet('');     // Please provide a name
greet('Alice'); // Hello, Alice
  

Boolean in Logical Expressions

  // AND returns first falsy or last value
console.log(true && 'yes');   // 'yes'
console.log(false && 'yes');  // false

// OR returns first truthy or last value
console.log(null || 'default'); // 'default'
console.log('hi' || 'default'); // 'hi'
  

Common Mistakes

Empty Array vs Empty String

  // [] is truthy — this does NOT detect empty array
if ([]) {
    console.log('Runs!'); // unexpected for beginners
}

// Correct empty check
const arr = [];
if (arr.length > 0) { /* has items */ }
  

Document.all (Legacy Edge Case)

In very old browsers, document.all was both truthy and falsy. Modern code should not rely on this.

Boolean Methods and Properties

Booleans are primitives, but you can call methods via autoboxing:

  true.toString();   // 'true'
(false).valueOf(); // false
  

Strict Equality with Booleans

  console.log(true == 1);   // true (coercion)
console.log(true === 1);  // false (no coercion)
console.log(false == 0);  // true
console.log(false === 0); // false
  

Always use === unless you explicitly need type coercion.

Real-World Patterns

  // Feature flags
const features = { darkMode: true, beta: false };
if (features.darkMode) enableDarkTheme();

// Form validation
function isValidEmail(email) {
    return typeof email === 'string' && email.includes('@') && email.length > 3;
}

// Optional chaining with boolean checks
const user = { settings: { notifications: true } };
if (user?.settings?.notifications) {
    sendPushNotification();
}
  

Troubleshooting

Symptom Likely Cause Fix
0 treated as missing || with numeric zero Use ?? instead
'false' string is truthy Non-empty string Compare explicitly: value === 'true'
Unexpected if branch Object/array always truthy Check .length or Object.keys().length

Exercises

  1. Write a function toBool(value) that returns true only for true, 1, 'true', '1' — everything else false.
  2. Predict the output of: console.log(!!'0', !!0, !!null, !![]).

Understanding truthy/falsy behavior helps you write concise conditions and avoid subtle bugs with values like 0 and ''.