beginner22 min

Operators and Conditionals

Compare values and branch your program's logic with if/else.

What you'll learn

  • Use comparison and logical operators to build boolean expressions
  • Write if/else if/else chains to branch program logic
  • Explain truthy and falsy values

Prerequisites

Explanation

Operators combine or compare values into a new result. You already know arithmetic operators (+, -, *, /). Two other families matter just as much:

Comparison operators produce a boolean: === (strictly equal), !== (strictly not equal), <, >, <=, >=. Always prefer ===/!== over ==/!= — the loose versions silently convert types before comparing ("5" == 5 is true), which causes subtle bugs. The strict versions require both type and value to match.

Logical operators combine booleans: && (and — both sides must be true), || (or — at least one side must be true), ! (not — flips a boolean).

const age = 20;
const hasTicket = true;
const canEnter = age >= 18 && hasTicket;

Conditionals let your program take different paths depending on a condition:

if (age >= 18) {
  console.log("Adult");
} else if (age >= 13) {
  console.log("Teenager");
} else {
  console.log("Child");
}

Only the first matching branch runs. else if chains are checked top to bottom, and else catches everything else.

JavaScript also has the idea of truthy and falsy values: outside of actual booleans, values are automatically treated as true or false in a boolean context (like an if condition). Exactly these values are falsy: false, 0, "" (empty string), null, undefined, and NaN. Everything else — including "0" (a non-empty string!) and empty arrays/objects — is truthy. This is why if (username) is a common, convenient way to check "is this non-empty," but it can also trip you up if a legitimate value like the number 0 is falsy in a context where you meant something else.

Example

A branching decision using comparison and logical operators.

const temperature = 15;
const isRaining = false;

if (temperature > 25 && !isRaining) {
  console.log("Great day for a walk.");
} else if (isRaining) {
  console.log("Bring an umbrella.");
} else {
  console.log("A bit chilly, grab a jacket.");
}

Try it yourself

Change the temperature and isRaining values and see which branch runs.

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Guided exercise

Guided exercise

Write a function `classify(score)` that returns "pass" if score is 60 or above, otherwise returns "fail".

Checks: classify(75) returns "pass" · classify(40) returns "fail" · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Independent exercise

Independent exercise

Write a function `ticketPrice(age, isStudent)` that returns 0 for age under 5, 8 for a student of any other age, and 12 otherwise.

Checks: Under-5 pays 0 · Student pays 8 · Adult non-student pays 12 · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Common mistakes

  • Using = (assignment) instead of === (comparison) inside an if condition.
  • Relying on == and getting surprised by type coercion, e.g. `0 == ""` being true.
  • Forgetting that only the first matching branch in an if/else if chain runs — order matters.

Knowledge check

Knowledge check

1. Why is === generally preferred over == in JavaScript?
2. Which of these values is falsy in JavaScript?
3. In an if/else if/else chain, how many branches can run?

Takeaway

Comparisons build booleans, and if/else chains use those booleans to choose exactly one path.

Summary

Strict comparison operators (===, !==) avoid type-coercion bugs, logical operators (&&, ||, !) combine conditions, and if/else if/else chains run exactly one matching branch. Falsy values (false, 0, "", null, undefined, NaN) matter when a condition isn't an explicit boolean.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.

Next: Loops