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:

  1. var umbrellaMandatory;
  2. if(country === 'England'){
  3. umbrellaMandatory = true;
  4. } else {
  5. umbrellaMandatory = false;
  6. }

The else clause can be joined with another if. Lets remake the example from the previous article:

  1. if(country === 'England') {
  2. ...
  3. } else if(country === 'France') {
  4. ...
  5. } else if(country === 'Germany') {
  6. ...
  7. }

{% exercise %}
Fill up the value of name to validate the else condition.
{% initial %}
var name =

if (name === “John”) {

} else if (name === “Aaron”) {
// Valid this condition
}
{% solution %}
var name = “Aaron”;

if (name === “John”) {

} else if (name === “Aaron”) {
// Valid this condition
}
{% validation %}
assert(name === “Aaron”);
{% endexercise %}