Academy
ProgrammingJS for BeginnersConditionals

Conditionals

The Fork in the Road

Every program you have written so far runs the same way every time. Same input, same output, no variation. That is useful for a calculator. It is useless for anything that needs to respond to the world.

Real programs need to ask questions:

  • Is the user logged in?
  • Is the score above 90?
  • Did the network request succeed?

Based on the answer, they take a different path. This is a conditional — a fork in the road where the program chooses a direction based on whether something is true or false.

if

The simplest form:

The structure is:

if (condition) {
  // runs only if condition is true
}

The condition inside () is evaluated. If it produces true, the block inside {} runs. If it produces false, the block is skipped entirely. Either way, the program continues after the closing }.

Change temperature to 36 and run again. The message disappears. The "Check complete" line always runs regardless.

if / else

What if you want to do something different when the condition is false?

Exactly one of the two blocks will run — never both, never neither. The else block is the default path when the condition is false.

if / else if / else

Often you have more than two possibilities:

JavaScript evaluates each condition from top to bottom and runs the first block whose condition is true — then skips all the rest. The final else is the catch-all: it runs only if every condition above was false.

Try changing marks to different values: 95, 80, 65, 45, 30. Watch which branch runs each time.

Comparison Operators

The condition inside if () can use any expression that produces a boolean. The most common tools:

You already know from the previous lesson: always use === and !==, never == and !=. The strict versions compare both value and type. The loose versions silently coerce and produce surprises.

Logical Operators: Combining Conditions

Sometimes one condition is not enough. You need to ask two things at once.

  • && (AND) — true only if both sides are true
  • || (OR) — true if either side is true
  • ! (NOT) — flips a boolean

A useful way to think about it: && is strict (both), || is generous (either).

Truthy and Falsy

Here is something JavaScript-specific that surprises everyone: you don't need a strict true or false inside an if. JavaScript will accept any value and decide whether to treat it as true or false.

Values that behave like false are called falsy. Everything else is truthy.

The complete list of falsy values in JavaScript — memorise these:

Only the last four lines print. Every other value — including empty arrays and empty objects — is truthy.

This is extremely useful in practice. Try changing "" to your name and run again:

Instead of writing username !== "" && username !== null && username !== undefined, you just write if (username). The falsy check handles all the empty cases at once.

The Ternary Operator

For simple two-way decisions, there is a compact one-line form:

The structure is:

condition ? valueIfTrue : valueIfFalse

Read the ? as "then" and the : as "otherwise": age >= 18 ? "adult" : "minor""age is at least 18, then adult, otherwise minor."

Use the ternary for simple assignments. For anything with multiple lines of logic, use if/else — clarity matters more than brevity.

switch

When you are comparing one variable against many specific values, switch can be cleaner than a long if/else if chain:

Two things to notice:

break — Without it, JavaScript falls through to the next case and keeps running. This is almost never what you want. Always include break at the end of each case unless you are deliberately stacking cases (like the weekday grouping above).

default — The catch-all, equivalent to else. It runs if no case matched.

switch uses === internally, so type matters. switch(1) will not match case "1".

A Complete Example

Put it all together: a simple program that categorises a student's performance.

This uses a function — the subject of the next lesson — but you can read it clearly: each student goes through the same decision logic and gets a different outcome based on their data.

Change the numbers. Add a student of your own. See how the conditions route each case.

What You Have Learned

Conditionals are how a program responds to the world rather than just executing blindly.

The key ideas:

  • if / else if / else evaluates conditions top to bottom and runs the first true branch
  • === and !== for comparisons — never == or !=
  • && (AND), || (OR), ! (NOT) for combining conditions
  • Every value is either truthy or falsy — the six falsy values are false, 0, "", null, undefined, NaN
  • Ternary condition ? a : b for compact two-way assignments
  • switch for comparing one value against many specific cases — always break

In the next lesson: Functions. You have already seen one (classify above) and used several (console.log, Number, parseInt). Now you will understand what they truly are — and why functions are the single most important concept in all of programming.