The else if
construct allows you to
set conditions in the else
block.
Let's look at an example:
let num = 1;
if (num == 1) {
console.log('value1');
} else if (num == 2) {
console.log('value2');
} else if (num == 3) {
console.log('value3');
}
The advantage of using else if
instead
of several ifs is the ability to catch the
situation when the value of the variable
num
does not match any of the conditions:
let num = 1;
if (num == 1) {
console.log('value1');
} else if (num == 2) {
console.log('value2');
} else if (num == 3) {
console.log('value3');
} else {
console.log('incorrect value of the variable num');
}
The variable day
contains some number
in the range from 1
to 31
.
Determine in which decade of the month this
number falls (in the first, second or third).
Modify the previous task so that if the
variable day
is not a number from
1
to 31
, an error message
would be issued.