typescript · 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 true, the lines inside run. If not, they are skipped.
To ask whether two values match, use three equals signs, . It gives true or false. One = puts a value in a box, so it cannot ask questions.
The lines that belong to the if sit inside curly brackets, { and }. That group is a . Indent the lines inside by two spaces, so people can see where it starts and ends.
let day: string = "Saturday";
console.log(day === "Saturday");
if (day === "Saturday") {
console.log("Pancakes for breakfast!");
}
console.log("Time to get dressed."); Lines after the closing curly bracket 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: string = "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 will not start. Can you fix it?
- Read the error:
TS1005: '(' expected.Which line does it point to? - The condition of an
ifalways goes inside round brackets. - Change line 2 to if (answer === "Paris") { then 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. - The condition goes in round brackets, the block in curly ones.
new words in your collection
- condition
- ===
- block
This lesson isn’t saved. A free account remembers every lesson you finish.