javascript · level 1 · unit 4 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An if runs code only when its is true.
Picture a fork in the path with a sign: Is it Saturday? An if asks a condition like that, inside round brackets. If the answer is yes, JavaScript runs the lines inside. If not, it walks straight past.
Every condition gives true or false. A value that can only be one of these two is a . To ask whether two values match, use three equals signs, ===. One = puts a value in a box.
The lines that belong to the if sit inside curly brackets { }, called a . Indent each line inside by two spaces, so people can see where it belongs.
let day = "Saturday";
console.log(day === "Saturday");
if (day === "Saturday") {
console.log("Pancakes for breakfast!");
}
console.log("Time to get dressed."); Lines after the closing } sit outside the if, so they run every time.
guess first
Rain or shine? Guess what this shows.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let weather = "sunny";
if (weather === "rainy") {
console.log("Take a coat.");
}
console.log("Off you go!");
your turn
Your turn. Get past the robot door.
The robot only opens for its owner. Type your name in both boxes, spelled exactly the same, then press run.
fix it
The quiz has crashed. Can you fix it?
- JavaScript cannot read the
ifline. Compare it with the example at the top. - The condition after
ifmust sit inside round brackets. - Change line 2 to if (answer === "Paris") { and press run.
nailed it
your code can make choices now. :D
ifruns its block only when the condition istrue.- Use
===to ask whether two values match. - Put the condition in round brackets and the lines in curly ones.
new words in your collection
- condition
- boolean
- block
This lesson isn’t saved. A free account remembers every lesson you finish.