← Back to Chapters

JavaScript Booleans

? JavaScript Booleans

⚡ Quick Overview

In JavaScript, a Boolean represents one of two values: true or false. Booleans are used heavily in conditions, comparisons, and decision-making structures like if, while, and the ternary operator.

You’ll most often see Booleans as the result of comparing values, or as flags that describe a state (for example, isOnline, isAdmin, hasPermission, etc.).

? Key Concepts

  • Boolean literals: true and false.
  • Comparison results: expressions like 10 > 5 produce Boolean values.
  • Truthy and falsy values: non-Boolean values that act as true or false in conditions.
  • Boolean() conversion: explicitly convert values to true or false.
  • Booleans in conditions: used in if, else, loops, and ternary operators.

? Syntax and Theory

A Boolean variable is typically created using let, const, or var and assigning it either true or false:

? Boolean Variable Declaration
let isOnline = true;
let hasPermission = false;

Most Booleans you work with come from expressions such as comparisons:

? Booleans from Expressions
console.log(10 > 5);      // true
console.log(3 === 4);     // false
console.log("a" !== "b"); // true

? Truthy and Falsy Values

In JavaScript, values are automatically converted to Booleans in contexts like if conditions. These values are classified as truthy or falsy.

  • Falsy values: false, 0, "" (empty string), null, undefined, NaN.
  • Truthy values: all other values (including [] and {}).
? Truthy and Falsy in Conditions
if ("Hello") {
  console.log("This is truthy!");
}

if (0) {
  console.log("This won't run."); // 0 is falsy
}

? Boolean() Conversion

The built-in Boolean() function converts any value to its Boolean equivalent:

? Using Boolean() for Conversion
console.log(Boolean(0));        // false
console.log(Boolean("hello"));  // true
console.log(Boolean(""));       // false
console.log(Boolean([]));       // true

This is handy when you want an explicit true or false rather than relying on JavaScript's automatic coercion.

? Booleans in Conditions

Booleans are at the heart of decision-making structures. For example, checking if a user is old enough to vote:

? Booleans in if/else
let age = 20;

if (age >= 18) {
  console.log("You can vote.");
} else {
  console.log("Too young.");
}

The condition age >= 18 evaluates to true or false, which then controls which block of code runs.

? Live Output / Explanation

Based on the examples above:

  • console.log(10 > 5); prints true because 10 is greater than 5.
  • console.log(3 === 4); prints false because 3 is not strictly equal to 4.
  • if ("Hello") { ... } runs its block because non-empty strings are truthy.
  • if (0) { ... } does not run because 0 is falsy.
  • Boolean("hello") returns true, while Boolean("") returns false.

Understanding these outputs helps you predict how your code will behave in different conditions.

? Tips and Best Practices

  • Use Boolean() when you want an explicit conversion instead of relying on implicit coercion.
  • Memorize common falsy values: false, 0, "", null, undefined, NaN.
  • Remember that empty arrays [] and objects {} are truthy, even though they are “empty”.
  • Prefer strict comparisons (=== and !==) to avoid unexpected type coercion.
  • Use descriptive Boolean variable names like isLoggedIn, hasPermission, or isAdmin.

? Try It Yourself

  • Check the Boolean value of null, [], and {} using Boolean().
  • Create a flag isLoggedIn and write an if/else block that prints a welcome message only if it is true.
  • Write a program that:
    • Asks for a user's age.
    • Stores the result of age >= 18 in a Boolean variable.
    • Uses that variable in an if/else statement to decide what message to print.
  • Practice writing comparisons that return Booleans, such as score >= 50 or username === "admin".