3.3 If, Else, Else if

Why Do We Need This?

With the use of if and else, there is a lot more we can do with JavaScript! Conditionals become much more useful. It gives the program more control over what happens in the program by giving it different paths depending on the conditionals we decided to give it.

Basic Structure

This is the very basic idea of a  and  statement.

This is the very basic idea of a if and else statement.

If you think about it, we use conditional statements in real life. Here's an example:

If time is greater than 8, wake up. Anything else, go back to sleep.

<aside> ⚠️ There will be methods that aren't implemented in the code. Just imagine that the method was already made.

</aside>

Here's how you create a if statement.

if (conditonal) {

}

Here's an example of a if statement in action:

let currentTime = 8;

if (currentTime > 8) {
	wakeUp();
}

Here's how you create a else statement:

if (conditional) {

} else {

}

Here's an example of a else statement in action:

let currentTime = 8;

if (currentTime > 8) {
    wakeUp(); 
} else {
    goToSleep(); 
}

However, there are times where we want even more control. That means the use of the else if statement.