Deep Tech Point
first stop in your tech adventure

Else keyword in JavaScript – what you should know about it and should you really stop using it?

December 11, 2021 | Javascript

In JavaScript “else” keyword is a part of conditional statements that we use to perform specific actions which are based on different conditions. There are two conditionals that use the else keyword in their statements – “else” and “else-if”. In this article, we will explore the world of these two conditionals – when to use them, should you stop using them, and whether there are better alternatives available.

If-else and else-if conditional statements

The If-else conditional statement

We use the if-else when we want to define a block of code that needs to be executed if that same condition is false. The syntax looks like this:

if (condition) {
  //  a block of code that needs to be executed if the condition is true
} else {
  //  a block of code that needs to be executed if the condition is false
}

Now, let’s take a look at a simple example – we tell JavaScript if it’s less than 9 am, generate a Good morning greeting, else (or if the first condition is false – if it’s 9 am or more than 9 am) generate Hello greeting. Here’s the code:

if (hour < 9) {
  greeting = "Good morning";
} else {
  greeting = "Hello";
}

The Else-if conditional statement

We use the else-if conditional statement when we want to define a new condition to be executed if the first condition is false. You can use as many else if conditions as you want. Here's the syntax:

if (first_condition) {
  //  a block of code that needs to be executed if the first condition is true
} else if (second_condition) {
  //  a block of code that needs to be executed if the first condition is false and a second condition is true
} else {
  //  a block of code that needs to be executed if both the first and second conditions are false
}

Let's expand on the example we used above and say when the time is less than 9 am, we will be greeted with "Good morning". That's the first condition. And when the time is between 9 and 18, when we are dealing with the second condition, the greeting will be "Good day" and when the time is more than 18 - when both the first and second conditions are false, the greeting will be "Good evening".

if (time < 9) {
  greeting = "Good morning";
} else if (time < 18) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}

So, should you really stop using else keyword with conditionals?

Some programmers claim using else and else-if keyword with conditionals is something that you should avoid like plague. But, is this really the case? Let's explore that.

First, we need to understand what is the logic behind "else" and "else-if" conditional. It means - if it's X do this, if it's not X do that. Yes, this is super clean when our options are limited - when we have only two cases, or as in an example above when we have 3 possible outcomes. But what happens when we deal with larger variables? Our negative problem space (if it's not X do that) becomes much larger and sometimes this negative problem space can be difficult to understand and later on maintain the code. But is this really the reason to avoid else-if statements?

An alternative to else-if conditionals would be to only use if statements. When using this approach, you have to make sure the entry criteria for every "if" you define cannot happen simultaneously. That will force you to write down all possible conditions and predict all possible outcomes, which is actually the case with else-if, too. This way you will have a few special cases and you will promote the main execution lane without any dependence on the order of execution.

Another alternative to else-if would be to use a switch-case statement. However, this is not always possible and our example presents one of these scenarios, so don't try to rewrite the code above with a switch-case statement. However, when you can substitute else-if conditional with a switch-case statement, there are a few things you should pay attention to. Make sure you align each case with a switch and indent the code with each case. In addition to that, it is also super important to end each case with a break. This way you will ensure the program breaks out of switch once it executes the matched statement and the program continues execution at the statement following switch. Carefully think through all possible scenarios to avoid eventual fall-throughs. It also makes sense to end the switch-case statement with a default case, just to make sure that if none of the provided cases matches, the final result will somehow make sense.

The else keyword - what is the take-away message?