So far a program always runs every line. In real life we make decisions: if the science mark is 100, the allowance goes up. The if statement lets a program decide in the same way.
Write if, then the condition in brackets ( ), then the commands inside curly braces { }. Those commands run only when the condition is true; anything after the } runs either way:
let science = 100;
if (science == 100) {
console.log("Increased allowance!");
}
console.log("Report checked");Increased allowance! Report checked
A condition actually has a value: either true or false. if runs its commands when the value is true. You can print a condition to see its value:
let science = 80; console.log(science == 100); console.log(science == 80);
false true
==. A single = is for assigning (putting a value into a variable), not for asking “is it equal?”.else: what if the condition is false?
Add else after the } to say what runs when the condition is false. This way exactly one of the two parts always runs:
let science = 80;
if (science == 100) {
console.log("Increased allowance!");
} else {
console.log("Weeding the garden");
}Weeding the garden
You can compare text too, and the comparison treats capital and small letters as different. Each part can also go on one short line, as in the school book: if (x == 1){console.log("One");}; both styles give the same result.
let password = "cairo2026";
if (password == "Cairo2026"){console.log("Welcome");}
else {console.log("Wrong password");}Wrong password
else has no condition: else (x == 5) is wrong. And never put a semicolon right after the condition, as in if (x == 1);, because that cuts the condition off from its commands.Practice
Declare the variable math and assign it 100. If it equals 100, print Get a reward.
Press Run to see the result. Fill in the two blanks: if egg equals 0, print Let's go buy some., otherwise print I still have some.
let egg = 10;
___ {
console.log("Let's go buy some.");
} ___ {
console.log("I still have some.");
}Press Run to see the result. Declare the variable num and assign it 777. If it equals 777, print lucky; otherwise print better luck next time.
Press Run to see the result. Declare the variable num and assign it 5. If it equals 10, print Win; otherwise print Lose.
Press Run to see the result. This program prints nothing because the condition is false. Add an else that prints No reward when science is not 100.
let science = 80;
if (science == 100) {
console.log("The reward is a cake");
}Press Run to see the result. Declare the variable number and assign it 17. Use % to check if it is even: if the remainder after dividing by 2 equals 0, print 17 is even, otherwise print 17 is odd. Join the variable to the text instead of typing 17 inside the text.
Hint
The condition:number % 2 == 0Press Run to see the result. In a 20-question quiz a student got 18 right. Store total = 20 and correct = 18, and work out the number of mistakes in a variable mistakes. If mistakes equals 0, print Full mark!; otherwise print Mistakes: 2 (join the variable to the text).
Press Run to see the result. Key points
if (condition) { ... }runs its commands only when the condition istrue.else { ... }runs when the condition isfalse, and has no condition of its own.==compares;=assigns.