Back to If / Then Builder
Code Studio · Level 3

If / Then Challenge

Create a boolean variable and an if statement that runs only when the condition is true.

10-15 min Ages 9-12 Challenge JavaScript

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. 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. 2

    Booleans are lowercase, no quotes

    JavaScript only understands true and false written exactly like that — True with 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. 3

    Your challenge missions

    Make your own boolean and an if that speaks only when it's true. Then flip it to false and prove the robot stays quiet. Bonus mission: write two booleans and two if statements — one true, one false — and predict which message the robot will say before you press Run.

Code Editor

Write your own choice.

Example code
let hasMagicMap = true;

if (hasMagicMap) {
  say("Show the secret path.");
}
Output

Program result

Robot ready to speak your strings

Type real JavaScript, then run it.