Skip to content

Conditions (if/else)

Learn how programs make decisions with if, else if, and else — the foundation of branching logic.

What you'll learn

  • How if/else statements create branches in your code
  • What a boolean expression is and how to write one
  • How to chain multiple conditions with else if
  • Common comparison and logical operators

Concept

Conditions (if/else)

Conditions let your program make decisions. An if statement tests a boolean expression — something that is either true or false — and runs a block of code only when the test passes.

The if Statement

if (temperature > 30) {
  console.log("It's hot outside.");
}

If temperature > 30 is true, the block runs. If false, the program skips it entirely.

Adding else and else if

else provides a fallback that runs when the condition is false. else if lets you test a sequence of conditions in order:

if (score >= 90) {
  grade = "A";
} else if (score >= 80) {
  grade = "B";
} else if (score >= 70) {
  grade = "C";
} else {
  grade = "F";
}

The program checks each condition top to bottom and runs only the first one that matches. Order matters: put the most specific checks first.

Comparison and Logical Operators

  • Comparison: === (equal), !== (not equal), >, <, >=, <=
  • Logical: && (and), || (or), ! (not)
if (age >= 18 && hasTicket) {   // both must be true
  enterVenue();
}

Always prefer === over ==. The triple-equal checks value and type without surprising coercions.

Example

              const hour = 14;   // 24-hour format

let period;
if (hour < 12) {
  period = "morning";
} else if (hour < 18) {
  period = "afternoon";
} else {
  period = "evening";
}
console.log(`Good ${period}!`);   // "Good afternoon!"

// Logical operators combine conditions:
const isWeekend = false;
const isHoliday = true;
if (isWeekend || isHoliday) {
  console.log("No alarm today!");
}
            

The first block classifies the hour into a period. The second uses || (or) so the alarm is skipped if either condition is true.

Try it

  • Password Strength Analyser

    This tool uses conditions internally: if length > 12, if has uppercase, if has symbol... each adds to the score.

Common mistakes

The mistake

Using == instead of === for equality checks.

The fix

Use === (strict equality). It avoids type coercion surprises like 0 == '' being true. Reserve == for rare, intentional coercions.

The mistake

Checking conditions in the wrong order (broad before specific).

The fix

Order conditions from most specific to most general. Otherwise a broad check swallows the cases a specific check should handle.

Related Cheatsheets

Practice Conditions (if/else)

2 exercises

Practice Conditions (if/else)

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors