Learning to Code: Day 46 — Basic JavaScript Part 11

Hello everyone, hope you are all keeping well! Today we’re going to look at various Else Statements, so let’s get cracking! Cheers to FreeCodeCamp for the lessons.

Else Statements

-FreeCodeCamp

Note: The else statement has it’s own curly brackets and is written directly after it’s preceding if statement.

Else If Statements

-FreeCodeCamp

The Logical Order of Else If Statements

In the following examples, the two functions foo and bar both use a different order of opening statements, the first beginning with (x < 1) and the second beginning with (x < 2):

-FreeCodeCamp
-FreeCodeCamp

And the result of both:

-FreeCodeCamp

As we can see, although almost identical, the first statement in the code takes precedence over the following statements as JS reads the code from top to bottom, and delivers completely different answers when x is equal to 0.

Chaining If Else Statements

-FreeCodeCamp

Aaaand let’s call it there today, keeping it short, as we’ll be moving onto bigger concepts next. Til then!

Step by step, learning to code with FreeCodeCamp