if statements
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Making Decisions in Code
Until now, your code always runs every line, from top to bottom. But a real program needs to make decisions. Should Pyrochu attack? Is Aquachu's HP too low? Can your Codémon evolve?
The if statement lets you run a block of code only when a condition is true. Remember booleans? This is where they become powerful!
let pyrochuLevel = 25;
if (pyrochuLevel >= 20) {
console.log("Pyrochu is ready to evolve!");
}
console.log("Battle continues...");The message about evolving only appears because pyrochuLevel >= 20 is true. Try changing the level to 10 and run again: the first message disappears, but the last line still runs.
Exercise 2 — Anatomy of an if Statement
An if statement always has three parts:
if (condition) {
// code that runs when the condition is true
}1. The keyword if
2. A condition between parentheses ( ) — anything that gives true or false
3. A block between curly braces { } — the code to run
let aquachuHP = 15;
let maxHP = 100;
if (aquachuHP < 30) {
console.log("Aquachu is in danger!");
console.log("Use a potion quickly!");
}Exercise 3 — Will it Run?
What will this code print?
let zapEnergy = 80;
if (zapEnergy > 50) {
console.log("Thunder attack!");
}
console.log("Turn over");Thunder attack! then Turn overTurn overThunder attack!Hint : First evaluate the condition: is it true or false? Then remember that code after the if block always runs.
Exercise 4 — Complete the if Statement
Complete this code so Florachu drinks a potion when its HP drops below 25.
Hint : You need the keyword that starts a condition, a comparison operator meaning "below", and the character that closes a block.
Exercise 5 — Conditions Use Everything You Know
Any expression that produces a boolean can be a condition: comparisons, logical operators, even a boolean variable directly.
let level = 32;
let hasStone = true;
// A logical operator in a condition
if (level >= 30 && hasStone) {
console.log("Evolution unlocked!");
}let hasStone = true;
// A boolean variable used directly
if (hasStone) {
console.log("The stone glows...");
}Notice if (hasStone): no comparison needed! The variable is already a boolean, so it works as a condition on its own.
Truthy and Falsy Work Too
Remember truthy and falsy values? A condition doesn't have to be exactly true or false — JavaScript converts it. if ("Pyrochu") runs (non-empty string is truthy), if (0) doesn't (0 is falsy).
Exercise 6 — Which Condition Triggers?
Rockachu is level 12 with 40 HP. Which of these conditions is true?
let level = 12;
let hp = 40;level >= 10 && hp > 30level > 15hp === 50level >= 15 || hp < 30Hint : Check each side separately. With && both sides must be true, with || at least one side must be true.
Exercise 7 — Guard the Arena Door
The arena only accepts trainers who have a badge AND a Codémon of level 20 or more. Complete the condition.
Hint : "AND" means both requirements are needed. "20 or more" includes 20 itself.
Exercise 8 — Parts of an if Statement
Match each part of an if statement with its role.
if( ){ }level >= 20Exercise 9 — When the Condition is False
What will this code print?
let rockachuDefense = 45;
if (rockachuDefense > 100) {
console.log("Impenetrable armor!");
}
if (rockachuDefense > 40) {
console.log("Solid defense!");
}Solid defense!Impenetrable armor!Hint : Each if is independent. Evaluate each condition separately with the value 45.
Exercise 10 — Evolution Checker
Zap wants to evolve! Write the conditions:
1. If zapLevel is at least 25, print "Zap can evolve!"
2. If zapEnergy is strictly greater than 80, print "Zap is full of energy!"
3. If both conditions are true at the same time, print "MEGA EVOLUTION!"
let zapLevel = 27;
let zapEnergy = 95;
// Write your if statements here