Skip to content

Latest commit

 

History

History
62 lines (49 loc) · 1.47 KB

File metadata and controls

62 lines (49 loc) · 1.47 KB
chapter 5
pageNumber 42
description The else keyword used in conjunction with the if statement to provide an alternative code block to execute when the condition specified in the if statement evaluates to false.

Else

There is also an else clause that will be applied when the first condition isn’t true. This is very powerful if you want to react to any value, but single out one in particular for special treatment.

let umbrellaMandatory;

if (country === "England") {
  umbrellaMandatory = true;
} else {
  umbrellaMandatory = false;
}

The else clause can be joined with another if. Let's remake the example from the previous article.

if (country === "England") {
  ...
} else if (country === "France") {
  ...
} else if (country === "Germany") {
  ...
}

{% exercise %} From the following values write a conditional statement that checks if num1 is greater than num2. If it is, assign "num1 is greater than num2" to the result variable. If it is not, assign "num1 is less than or equal to num2".

{% initial %} let num1 = 10; let num2 = 5; let result;

// check if num1 is greater than num2 if( condition ) {

}else {

} {% solution %} let num1 = 10; let num2 = 5; let result;

// check if num1 is greater than num2 if (num1 > num2) { result = "num1 is greater than num2"; } else { result = "num1 is less than or equal to num2"; }

{% validation %} assert(result == "num1 is greater than num2" );

{% context %} {% endexercise %}