JavaScript Booleans

A JavaScript Boolean represents one of two values: true or false. Booleans are the foundation of all conditional testing and decision-making in JavaScript.

Boolean Values

The boolean type has only two values: true and false. They are often the result of comparisons and conditions.

Basic Booleans
let isActive = true;
let isDeleted = false;

console.log(isActive);          // true
console.log(typeof isActive);   // "boolean"

// Comparisons return booleans
console.log(10 > 5);    // true
console.log(10 < 5);    // false
console.log(10 === 10); // true
console.log(10 !== 10); // false

The Boolean() Function

The Boolean() function converts any value to a boolean. This is useful for explicitly checking the truthiness of a value.

Boolean() Conversion
// Truthy values convert to true
console.log(Boolean(100));        // true
console.log(Boolean("hello"));    // true
console.log(Boolean([]));         // true
console.log(Boolean({}));         // true
console.log(Boolean(" "));       // true (space is truthy!)
console.log(Boolean("false"));   // true (non-empty string!)

// Falsy values convert to false
console.log(Boolean(0));          // false
console.log(Boolean(-0));         // false
console.log(Boolean(""));        // false
console.log(Boolean(null));       // false
console.log(Boolean(undefined));  // false
console.log(Boolean(NaN));        // false

Truthy and Falsy Values

Every value in JavaScript has an inherent boolean value. There are only 6 falsy values — everything else is truthy.

Falsy ValuesDescription
falseThe keyword false
0, -0Zero and negative zero
""Empty string
nullAbsence of any value
undefinedVariable declared but not assigned
NaNNot a Number
Common Truthy/Falsy Gotchas
// These are all TRUTHY (might be surprising!)
console.log(Boolean("0"));      // true (non-empty string)
console.log(Boolean("false"));  // true (non-empty string)
console.log(Boolean([]));        // true (empty array)
console.log(Boolean({}));        // true (empty object)
console.log(Boolean(-1));        // true (non-zero number)
console.log(Boolean(Infinity)); // true

// Practical check: is string empty?
let input = "";
if (!input) {
  console.log("Input is empty"); // This runs
}

// Practical check: does value exist?
let user = null;
if (!user) {
  console.log("No user found"); // This runs
}

The !! (Double NOT) Operator

The double NOT operator (!!) is a shorthand way to convert any value to its boolean equivalent. The first ! converts to boolean and negates, the second ! negates back.

Double NOT Operator
// Single ! negates
console.log(!true);       // false
console.log(!0);          // true
console.log(!"hello");   // false

// Double !! converts to boolean
console.log(!!"hello");  // true
console.log(!!0);         // false
console.log(!!null);      // false
console.log(!!undefined); // false
console.log(!!1);         // true
console.log(!![]);        // true

// Practical use
let name = "Alice";
let hasName = !!name;
console.log(hasName); // true
console.log(typeof hasName); // "boolean"

Comparisons Return Booleans

All comparison operators return boolean values. You can store comparison results in variables and use them later in your code.

Boolean Results from Comparisons
let age = 25;
let isAdult = age >= 18;
let isSenior = age >= 65;
let isTeenager = age >= 13 && age <= 19;

console.log(isAdult);    // true
console.log(isSenior);   // false
console.log(isTeenager); // false

// Use boolean variables in conditions
if (isAdult && !isSenior) {
  console.log("Working age adult");
}

// Boolean in array methods
let numbers = [1, 2, 3, 4, 5, 6];
let evens = numbers.filter(n => n % 2 === 0);
console.log(evens); // [2, 4, 6]
📝 Note: Never compare to true or false directly (if (x === true)). Instead, just use the value: if (x). The exception is when you specifically need to distinguish true from other truthy values.
Exercise:
What does Boolean("") return?
Try it YourselfCtrl+Enter to run
Click Run to see the output here.