Decisions: if, else and switch
- 5 min reading
- 6 min activity
0 of 5 tasks doneup to 100 XP
Mission 4, step 5. Today your code learns to decide: one message for a new visitor, another when a form is empty.
- Compare values and combine conditions with
&&,||and!. - Write
if,else ifandelse, and the short ternary form. - Choose among many exact values with
switch.
In lesson 4.4 you measured text. One quick question.
Quick recall
Comparing values
A comparison asks a question about two values. The answer is true or false.
| Operator | Meaning | Example |
|---|---|---|
=== | Same value and same type (use this) | 5 === "5" is false |
== | Equal after changing the types | 5 == "5" is true |
!== / != | Not equal (with / without the type) | 5 !== "5" is true |
> < >= <= | Bigger, smaller, or equal | 10 >= 10 is true |
The slides say that == does not look at the type. More exactly, it first changes the types, then compares. That can surprise you, so use === and !==.
Logical operators join conditions. (The slide calls them comparison operators.) && (and): all parts must be true, like password right and user name right. || (or): one is enough, like cash or card. ! (not) turns true into false.
True or false?
if, else if and else
let points = 85;
if (points >= 90) {
console.log("Grade: A");
} else if (points >= 80) {
console.log("Grade: B");
} else {
console.log("Not passed yet. Try again.");
}
The first true condition wins, so the order matters. else catches the rest.
For a short choice between two values, use the ternary operator: condition ? value if true : value if false.
let result = points > 50 ? "Passed" : "Try again";
On the slide, the long and the short version stand in one block and create the same variables twice, which stops with an error. They are two separate examples.
Worked example: the cash machine
Katarzyna tests a simple cash machine program. The balance is 5000, and she wants to take out some money.
- Plan the rules
0 or less is invalid. More than the balance is too much. The rest is fine.
- Check in this order
iffor invalid,else iffor "not enough money",elsefor the good case. - Change the balance
In the good case,
balance -= amount;, then show the new balance.
The cash machine plan
The slide's code has a bug: the balance never changes. This counts for the Bug Hunter badge.
Slide bug: the money machine
This code comes from a course slide. It has a small bug. Find it and fix it.
Many exact values: switch
let day = 3;
switch (day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
case 3: console.log("Wednesday"); break;
default: console.log("Not a day number");
}
case 3: runs when day is exactly 3. break means "found it, stop": without it, the next case runs too. default runs when no case fits, like else. On the slide, the console.log() after default: stands inside a comment, so an unknown day prints nothing.
Day name
From slide 63 of the course: put the lines of the switch in order. Day 3 should print Wednesday and nothing else.
- Condition
- A question that is true or false, like
points >= 90. - Logical operator
- Joins conditions:
&&,||,!. - Ternary operator
- A short if-else that gives one of two values.
- switch
- Chooses a
caseby an exact value;breakstops.
In short
- Use
===and!==: they check the value and the type. &&needs all parts true,||needs one,!turns it around.if,else if,else: the first true condition wins.switchfor exact values, withbreakafter each case.
Functions and loops: write code once, use it many times.
Sources
- MDN Web Docs (2026). Equality comparisons and sameness.
- MDN Web Docs (2026). Expressions and operators (logical operators).
- MDN Web Docs (2026). Making decisions in your code: conditionals.
- MDN Web Docs (2026). Conditional (ternary) operator and switch.
End of the lesson
This lesson: 0 of 110 XP
Code Cards in this lesson: 8
===> <&&||ifelse? :switch