JavaScript / Making decisions

Decisions: if and else

Make a program run some lines only when a condition is true, and other lines when it is not.

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:

JavaScript
let science = 100;
if (science == 100) {
    console.log("Increased allowance!");
}
console.log("Report checked");
Console
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:

JavaScript
let science = 80;
console.log(science == 100);
console.log(science == 80);
Console
false
true
To compare, use two signs ==. 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:

JavaScript
let science = 80;
if (science == 100) {
    console.log("Increased allowance!");
} else {
    console.log("Weeding the garden");
}
Console
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.

JavaScript
let password = "cairo2026";
if (password == "Cairo2026"){console.log("Welcome");}
else {console.log("Wrong password");}
Console
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

1Write the programCurriculum practice

Declare the variable math and assign it 100. If it equals 100, print Get a reward.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Fill in the blankCurriculum practice

Fill in the two blanks: if egg equals 0, print Let's go buy some., otherwise print I still have some.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let egg = 10;
___ {
    console.log("Let's go buy some.");
} ___ {
    console.log("I still have some.");
}
Console
Press Run to see the result.
3Write the programCurriculum practice

Declare the variable num and assign it 777. If it equals 777, print lucky; otherwise print better luck next time.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
4Write the programCurriculum practice

Declare the variable num and assign it 5. If it equals 10, print Win; otherwise print Lose.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
5Edit the programCurriculum practice

This program prints nothing because the condition is false. Add an else that prints No reward when science is not 100.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let science = 80;
if (science == 100) {
    console.log("The reward is a cake");
}
Console
Press Run to see the result.
6Write the programExtra practice

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.

HintThe condition: number % 2 == 0
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
7Write the programExtra practice

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

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • if (condition) { ... } runs its commands only when the condition is true.
  • else { ... } runs when the condition is false, and has no condition of its own.
  • == compares; = assigns.