JavaScript IF statements - Part 2 - Else If

Published on
5 mins read
––– views

We can write more complex if statements. On part 2, we are going to discover else if and how can be use it to make our decision making more fool proof.

Possible use cases

It's Friday night, we are out with our homies to do some normal human stuff. Like socializing, being responsible and well behaving adults. You know, fun stuff.

Everyone in our group wants to do something else, some aren't even realistic. For example, Mike has some suggestions that make no sense. It is never a good idea to eat other people's brownies, Mike.

What is Else IF?

Sometimes, we have multiple options to pick from. Some options aren't realistic or not possible at the given time. Our favorite bakery might be out of donuts. If they are out of donuts, we might check else if they have pies or try a different bakery.


const donuts = false;
const pies = false;

if (donuts === true) {
  console.log('2 donuts please');
} else if (pies === true) {
  console.log('1 pie please');
} else {
  console.log('please tell me what you have');
}

First, we check if the bakery has donuts. Well, they are out. I wonder, what else do they have. Let's ask if they have pies? Pies are out too. I have given up, please tell me what's in available. Since they are out of donuts and pies, we will ask the baker, there is anything with the else statement.

In simpler terms,

  1. If is the main question
  2. else if is like asking what about this?
  3. else is I have given-up, whatever

With practice, this sequence will make more sense. This way of thinking can help us to understand the main difference between if else if and else.

Here is a another example you will find on every single tutorial.

let grade = 50;

if (grade > 90) {
  console.log('passed with A');
} else if (grade > 80) {
  console.log('passed with B');
} else {
  console.log('failed');
}

we enter the student grade and IF , ELSE IF and ELSE statements decide the output.

We can keep going with additional else if statements to console.log other letter grades like C and D.


Combining AND OR with Else If

By know we discovered that with combining else if and && || statements, we can create very detailed blocks of code.

Here is another example. We will return to our bakery, because I find those "grading" examples fairly boring...

let myMoney = 10;
//
let donut = true;
let donutPrice = 4;
//
let pie = true;
let piePrice = 11;

if (myMoney > piePrice && pie === true) {
  console.log('one pie please');
} else if (myMoney > donutPrice && donut === true) {
  console.log('a donut please');
} else {
  console.log('Do you have eclairs?');
}

On this example, first we check if we have enough money (myMoney) for a pie && (and) if we do, we will check if they have a pie for sale.

Looks like pie is above our budget, we check if we have enough money for a donut && if yes, we will ask if they have a donut for sale.

If we can't have a donut, we will simply ask if they carry eclairs

Looks like we have enough money to buy 2 donuts, so why not get 2 of them?

let myMoney = 10;
//
let donut = true;
let donutPrice = 4;
//

if (myMoney > donutPrice) {
  let numberOfDonuts = Math.floor(myMoney / donutPrice);
  console.log('May I have ' + numberOfDonuts + ' donuts please?');
} else {
  console.log('have a great day');
}

first, we check if we have enough money to buy a donut

if we do, we do some quick math to figure max number of donuts we can have.

On this example, myMoney/donutPrice (10/4) will give us 2.5. Most Bakeries won't sell half a donut.

Enter Math.floor(), this build in function will take the number (2.5) and round it down to nearest integer, which is 2. Integer = 1,2,3,4,5...

We can add some logic and math inside if statements. This is just an example. We will go into more details later on.


Imagine combining &&, ||, else if and some logic inside an if statement.

Your turn

Take everything you have learned here, and create a huge code block with many else if && || and add some logic and math inside those statements.

It doesn't matter if your story make sense or have any real life value. As you can see, my examples did not. It's okay, that's how we practice..