← Back to Concepts

Booleans & Conditions

Boolean values: true and false

A boolean is a value that can only be true or false. You use booleans to represent “yes/no” facts, like whether a light is on or whether a user has an account.
Later, booleans become the heart of conditions: an if statement checks a boolean to decide which code should run.

const isLightOn = true;
const hasAccount = false;

console.log(isLightOn);
console.log(hasAccount);

const typeOfBoolean = typeof isLightOn;

Key Points:

  • Meaning: Booleans represent simple yes/no facts in your program
  • Two values: A boolean is either true or false
  • Type check: typeof tells you a value is a "boolean"
  • Common use case: conditions — if (hasAccount) { ... } decides what code runs

Comparison operators: asking a question

Comparison operators help you ask “yes/no” questions about values. When you compare two values (like age and 18), JavaScript gives you a boolean: true if the statement is correct, or false if it isn’t.
Comparison operators are > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to), === (strictly equal to), !== (strictly not equal to).

const age = 25;
console.log("Is age exactly 21?", age === 21);
console.log("Is age at least 18?", age >= 18);
console.log("Is age not 30?", age !== 30);
console.log("Is age less than 18?", age < 18);

const name = 'John';
console.log("Is name 'John'?", name === "John");
const isBob = name === 'Bob';
const isNotAsh = name !== 'Ash';

Key Points:

  • Comparisons produce booleans: operators like ===, !==, >=, and < always evaluate to true or false
  • === is “exactly equal”: it checks that both values the same
  • !== is “not equal”: it checks that both values are different
  • Store answers: save a comparison into a variable like isBob when you want to reuse it
  • Common use case: conditions — if (age >= 18) { ... }

Logical operator &&: AND

The && operator means “AND”. It helps you combine multiple checks into one condition. The whole condition is true only when every part is true.

const isWeekend = true;
const isSunny = true;

const shouldGoOutside = isWeekend && isSunny;

const age = 15;
const isWithParent = false;
console.log( age > 14 && age < 18 && isWithParent );

Key Points:

  • AND means “all”: a && b is true only when both are true
  • Chain checks: a && b && c is true only when every part is true

Logical operator ||: OR

The || operator means “OR”. Use it when any one condition being true is enough.
JavaScript checks OR conditions from left to right, and it stops as soon as it finds a true. This is called short-circuiting.

const age = 14;
const doesntNeedJob = age < 18 || age > 68;

const isFirstClass = false;
const isDiamondStatus = false;
const hasAirlineCreditCard = true;

const boardsFirst = isFirstClass || isDiamondStatus || hasAirlineCreditCard;

Key Points:

  • OR means "any": a || b is true if at least one part is true
  • Left to right: JavaScript checks each part in order
  • Short-circuit: it stops as soon as it finds a true
  • Common use case: allow something if any requirement is met