If / Then Challenge
Create a boolean variable and an if statement that runs only when the condition is true.
If / then rules to remember
The recipe: if (question) { then do this } — the guard checks the
question in the parentheses, and the code in the braces runs only when the answer
is true.
-
1
Every symbol matters
The condition needs its parentheses
( )and the action needs both curly braces{ }. Miss one and JavaScript gets confused and reports an error. Count them like a coder: open, close, open, close. -
2
Booleans are lowercase, no quotes
JavaScript only understands
trueandfalsewritten exactly like that —Truewith a capital T breaks, and"true"in quotes is a string pretending to be an answer. Give your boolean box a name that sounds like a yes/no question:hasMagicMap,isHungry,canFly. -
3
Your challenge missions
Make your own boolean and an
ifthat speaks only when it's true. Then flip it tofalseand prove the robot stays quiet. Bonus mission: write two booleans and twoifstatements — one true, one false — and predict which message the robot will say before you press Run.
Write your own choice.
let hasMagicMap = true;
if (hasMagicMap) {
say("Show the secret path.");
}
Program result
- Run your code to see output.
Type real JavaScript, then run it.
You wrote JavaScript that checks a condition before running an action.
Next Lesson