If, Else
In the previous exercise, we wrote an if
statement that executes some code if its condition is true. However, we often need to do something if the condition is false. For this, we use the else
statement. For example:
let age = 15;
if (age > 17) { // if age is greater than 17, execute:
console.log("You can enter");
}
else { // otherwise, execute:
console.log("You cannot enter");
}
// Prints: You cannot enter
In the example, you can see that if age
is greater than 17
, it prints You can enter
. Otherwise (if age
is less than or equal to 17
), it prints You cannot enter
.
Instructions
Add an else
statement to the code that, when executed, will print I will not buy the monitor
.
Start programming for free
2/9