So far, every line of your code ran, from top to bottom. Real programs make choices: show an umbrella only when it rains, or a different section each day. In this lesson your code learns to decide.
flag
What you'll learn
How to compare values and get true or false.
How to run code only when something is true, with if, else if and else.
How to pick one choice out of many with switch.
waving_handWelcome to decisions
Control flow is the order in which your code runs. With if, else and switch, you control it:
some code runs only when a condition is true, and other code runs when it is false.
A condition is a question with a yes-or-no answer, like "is the rain chance above 50?". In JavaScript the answer is a boolean: true or false, which you met in JS Lesson 2.
traffic
Think of a jeepney driver at a traffic light. If the light is red, the driver stops (if).
If it isn't red but it's yellow, the driver slows down (else if). For any other light, the driver goes (else).
The driver checks in that order and does only one of them.
Red lightThe driver stops.if (light === "red") { stop(); }
Yellow lightIf it is not red, but yellow: slow down.else if (light === "yellow") { slowDown(); }
Any other lightIf it is neither: go.else { go(); }
Now let's see the code ↓
Red lightThe driver stops.if (light === "red") { stop(); }
Yellow lightIf it is not red, but yellow: slow down.else if (light === "yellow") { slowDown(); }
Any other lightIf it is neither: go.else { go(); }
The checks happen from top to bottom. As soon as one is true, its code runs, and the rest are skipped.
searchA closer look
Comparing values
A comparison asks a question about two values and gives back true or false.
> and < mean greater and less than, and >= and <= include the value itself.
To ask "are these the same?", use three equals signs: ===. Its opposite is !==, "not the same".
Remember that one = means "put this value in", from JS Lesson 2. The common mistakes below show what happens when they get mixed up.
How an if statement is written
ifkeyword
(rain > 50)condition (true or false)
{ ... }runs only when true
The lines inside { } are called a block. They are indented two spaces, so you can see at a glance which lines belong to the if. JavaScript doesn't need the spaces, but people do.
Checking more than one thing
&& means and: both sides have to be true. || means or: one true side is enough.
! means not and flips true to false.
So day === "Saturday" || day === "Sunday" is true on both weekend days.
switch: one value, many choices
When you compare one value against many exact choices, switch is easier to read than a long chain of else if.
Each case is one choice. When the value matches, the code under it runs until a break.
default is like else: it runs when no case matches.
switch checks for an exact match, like ===. For ranges, like "more than 70", use if and else if.
constructionLet's build it
Let's make Front Page Bytes give weather advice and pick a section for each day. The HTML and CSS stay the same in every step: a heading and two boxes, #weather and #day.
Watch the boxes and the console as the JavaScript changes.
rain holds 80. Each line asks a question about it, and the console shows the answers:
80 is greater than 50 (true), not less than 50 (false), the same as 80 (true), and not "not the same" (false).
2Step 2: Run code only if something is true
Your code
let rain = 80;if (rain > 50) { document.getElementById("weather").innerHTML = "Bring an umbrella!";}
rain > 50 is true, so the line inside the { } runs and the weather box says Bring an umbrella!
Try picturing rain as 20: the condition would be false, and the box would keep its old text.
3Step 3: Do something else with else
Your code
let rain = 20;if (rain > 50) { document.getElementById("weather").innerHTML = "Bring an umbrella!";} else { document.getElementById("weather").innerHTML = "No umbrella needed today.";}
Now rain is 20, so the condition is false. The if block is skipped and the else block runs instead:
No umbrella needed today. One of the two always runs, never both.
4Step 4: More choices with else if
Your code
let rain = 40;let message;if (rain > 70) { message = "Heavy rain. Stay inside if you can.";} else if (rain > 30) { message = "Light rain. Bring an umbrella.";} else { message = "Clear skies today!";}document.getElementById("weather").innerHTML = message;
switch (day) compares day with each case. It matches "Wednesday", so section becomes Game reviews, and break leaves the switch.
If day were "Tuesday", no case would match, and default would give Top stories.
6Step 6: Share one answer between cases
Your code
let day = "Sunday";let section;switch (day) { case "Saturday": case "Sunday": section = "Weekend edition"; break; case "Monday": section = "Tech news"; break; default: section = "Top stories";}document.getElementById("day").innerHTML = day + ": " + section;
rain = 80 doesn't compare. It puts 80 into rain, so the condition counts as true and the umbrella message shows, even though the rain chance was 20.
The console shows the damage: rain is now 80.
tips_and_updatesTip: Compare with === (three equals signs). One = puts a value into a variable.
error
Forgetting break in a switch
The mistake
let day = "Monday";let section;switch (day) { case "Monday": section = "Tech news"; case "Wednesday": section = "Game reviews"; break; default: section = "Top stories";}document.getElementById("day").innerHTML = day + ": " + section;
90 is above 30, so the first block runs and says Light rain. JavaScript stops at the first true condition, so the rain > 70 check never gets a turn.
tips_and_updatesTip: Put the narrowest check first: test rain > 70 before rain > 30.
quizTest your knowledge
Write JavaScript in the JS tab. score and day are ready for you. Give a message for the score, like Excellent for 90 and up, then pick a club for the day with switch. Try changing score and day to test every choice.
auto_awesomeNo clicking needed. Each goal gets a check mark by itself once your code does it.
Use if to check the score.not done yet
Add an else if for another range of scores.not done yet
End with an else for every other score.not done yet
Show the result in #result.not done yet
Use switch with at least two cases to pick a club for the day, and show it in #club.not done yet