JavaScript

This page gently explains JavaScript Boolean values and how they are used to make decisions in a program.

Goal: understand true and false, then read conditions and comparison results with confidence.

Boolean values

A Boolean has one of two values: true or false. A Boolean often represents whether something is yes or no, on or off, or valid or invalid.

Use a Boolean in a condition

JavaScript

const isHungry = true;

if (isHungry) {
  console.log("Time to eat");
}

When isHungry is true, the body of the if statement runs. When it is false, it is skipped.

Comparisons produce Booleans

JavaScript

console.log(10 === 10); // true
console.log(10 === 9);  // false
console.log(5 > 3);     // true

The strict equality operator === compares both value and type. It is usually easier to reason about than the loose equality operator ==, which may convert types.

Truthy and falsy values

An if condition can receive any value. JavaScript treats values such as false, 0, an empty string, null, and undefined as falsy; many other values are truthy. When this implicit conversion could be confusing, compare explicitly or use Boolean(value) to show your intent.

Related topics