JavaScript: Switch vs. If Else

Michelle Wong
3 min readNov 25, 2019

--

Can’t decide?

Throughout my coding days in JavaScript, I constantly found myself debating whether to use a switch case or an if else statement for my conditional. So, naturally, I decided to write a blog to weigh out the differences (for me & you) and see which one is better in specific circumstances.

A switch statement is usually more efficient than a set of nested ifs. When you have to choose which one to use, it’s based on readability and the expression that the statement is testing.

Basically, an if else is used for taking a decisions while a switch statement is used to test the value of the given variable against a list of case value .

Differences Between If-Else & Switch

  • The expression inside of an if statement decides whether to execute the statements inside the if block or under the else block. For switch, the expression inside switch statement decides which case to execute.
  • The if-else statement checks for equality as well as for logical expression. On the other hand, switch checks only for equality.
  • The if statement evaluates integer, character, pointer or floating-point type or boolean type. On the other hand, switch statement evaluates only character or an integer datatype.
  • Sequence of execution is like either statement under if block will execute or statements under else block statement will execute. However, the expression in the switch statement decides which case to execute and if you do not apply a break statement after each case it will execute till the end of switch statement.
  • For an if-else statement, if the expression inside of the if turn outs to be false, the statement inside of the else block will be executed. For the switch statement, if the expression inside of the switch statement turns out to be false then the default statements are executed.
  • It’s known to be difficult to edit if-else statements since it’s tedious to trace where the correction is required. Many people agree that it’s much simpler to edit switch statements since they’re easy to trace.

Here’s the general layout of an if-else statement verses the switch case:

This is the general syntax of an if-else statement:

if (condition1) { //Body of if }

else if (condition2) { //Body of if }

else if (condition3) { //Body of if }

else { //default if all conditions return false }

And this is the general syntax for switch:

switch ( variable )

{

case <variable value1>: //Do Something

break;

case <variable value2>://Do Something

break;

default: //Default will perform if all case’s fail

break;

}

The if-else ladder is of type strict condition check, while switch is of type jump value catching.

Some key advantages of switch over if-else ladder:

  • A switch statement works much faster than an equivalent if-else ladder. It’s because the compiler generates a jump table for a switch during compilation. As a result, during execution, instead of checking which case is satisfied, it only decides which case has to be executed.
  • It’s more readable compared to if-else statements.

In the end, the choice is yours and I hope this blog helps lead you in the right path to making the most informed decision when to use an if-else statement verses a switch case!

--

--