We can add more conditions to our if...else
with an else if
statement. The else if
statement allows for more than two possible outcomes. You can add as many else if
statements as you’d like, to make more complex conditionals!
The else if
statement always comes after the if
statement and before the else
statement. The else if
statement also takes a condition. Let’s take a look at the syntax:
let stopLight = 'yellow'; if (stopLight === 'red') { console.log('Stop!'); } else if (stopLight === 'yellow') { console.log('Slow down.'); } else if (stopLight === 'green') { console.log('Go!'); } else { console.log('Caution, unknown!'); }
The else if
statements allow you to have multiple possible outcomes. if
/else if
/else
statements are read from top to bottom, so the first condition that evaluates to true
from the top to bottom is the block that gets executed.
In the example above, since stopLight === 'red'
evaluates to false
and stopLight === 'yellow'
evaluates to true
, the code inside the first else if
statement is executed. The rest of the conditions are not evaluated. If none of the conditions evaluated to true
, then the code in the else
statement would have executed.
Instructions
Let’s create a program that keeps track of the way the environment changes with the seasons. Write a conditional statement to make this happen!
In main.js there is already an if...else
statement in place. Let’s add an else if
statement that checks if season
is equal to 'winter'
.
Inside the code block of the else if
statement, add a console.log()
that prints the string 'It\'s winter! Everything is covered in snow.'
.
Add another else if
statement that checks if season
is equal to 'fall'
.
Inside the code block of the else if
statement you just created, add a console.log()
that prints the string 'It\'s fall! Leaves are falling!'
.
Add a final else if
statement that checks if season
is equal to 'summer'
.
Inside the code block of the else if
statement you just created, add a console.log()
that prints the string 'It\'s sunny and warm because it\'s summer!'
.