Every useful program eventually hits a decision point. Should this button submit the form or show an error? Does this user see the admin dashboard or the guest view? JavaScript answers these questions with conditional statements. They are the control valves of your code: open one path, close another, all based on whether an expression evaluates to true or false. Without them, your script would march from top to bottom without ever reacting to the person using it.

The Simplest Branch: if

The if statement is the gate you walk through first. You place a condition inside parentheses. If that condition resolves to true, the code block underneath executes. If it resolves to false, JavaScript skips the block entirely and continues down the file.

This is exactly what you need for simple, one-off checks. Imagine validating a form field before you send anything to a server:

const email = document.getElementById("email").value;

if (email === "") {
  alert("Please enter your email.");
}

No email, no submission. The logic is blunt and clear. Use if when there is only one special case to guard against and no alternate action required.

Handling Either–Or: if...else

Most real decisions have two sides. The if...else pair covers exactly that. One block runs when the condition is true, the other runs when it is false. The two paths are mutually exclusive.

Picture a dark-mode check on page load:

const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;

if (prefersDark) {
  document.body.classList.add("dark");
} else {
  document.body.classList.add("light");
}

The browser will never add both classes. It picks one path, executes it, and moves on. Whenever your logic splits cleanly into two possible outcomes, this is the structure to reach for.

Multiple Paths: if...else if...else

Not every problem is binary. Sometimes you need a ladder of possibilities. The if...else if...else chain lets you test several conditions in sequence. JavaScript evaluates them from top to bottom and runs the first block whose condition is true. After that match, the rest of the ladder is ignored.

A classic example is mapping a numeric score to a letter grade:

const score = 87;
let grade;

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

Order matters enormously here. If you checked score >= 70 before score >= 90, almost every passing grade would get trapped in the C bucket because the first true match wins. Keep your conditions ranked from most specific to least specific, and always cap the chain with a final else to catch anything that slips through.

Matching Fixed Values: switch

When you are comparing one variable against a list of known constants, a long if...else chain turns noisy. The switch statement cleans things up. It evaluates an expression once, then compares it against case labels using strict equality (===). This makes the code easier to scan when the options are discrete and named.

Consider setting store hours based on the current day:

const day = new Date().getDay();

switch (day) {
  case 0:
    console.log("Sunday hours: 10am - 4pm");
    break;
  case 6:
    console.log("Saturday hours: 9am - 6pm");
    break;
  default:
    console.log("Weekday hours: 8am - 8pm");
}

Be careful with break. If you omit it, execution falls through to the next case and keeps running. That behavior is occasionally useful, but more often it causes silent bugs. Used correctly, switch makes your intent obvious at a glance and keeps related branches aligned in one tidy column.

Compact Decisions: The Ternary Operator

The ternary operator—written as ? :—is an expression, not a statement. That distinction matters