DEV Community

Cover image for Switch Vs. If else
Amrita-padhy
Amrita-padhy

Posted on

2

Switch Vs. If else

As I learned JavaScript, I often found myself stuck between using switch or if-else statements. It's a common dilemma for beginners. Let's dive into these two choices together, exploring what they offer and when to use each one.

Readability: For simple cases where you're testing the value of a single variable against multiple conditions, switch statements can often be more readable and maintainable. They provide a clear structure for handling multiple cases.

switch (day) {
case 'Monday':
case 'Tuesday':
case 'Wednesday':
case 'Thursday':
case 'Friday':
console.log('Weekday');
break;
case 'Saturday':
case 'Sunday':
console.log('Weekend');
break;
default:
console.log('Invalid day');
}

Performance: In JavaScript, the performance difference between switch and if-else statements is generally negligible. Modern JavaScript engines are quite efficient in optimizing both constructs.

Flexibility: if-else statements provide more flexibility for complex conditional logic. You can use logical operators and test multiple conditions within a single if or else if block, which can sometimes be more concise and expressive.

if (score >= 90) {
console.log('A');
} else if (score >= 80) {
console.log('B');
} else if (score >= 70) {
console.log('C');
} else {
console.log('D');
}

Handling Different Types of Conditions: if-else statements are more versatile when dealing with conditions beyond simple value comparisons, such as ranges or boolean expressions.

if (age < 18) {
console.log('Minor');
} else if (age >= 18 && age < 65) {
console.log('Adult');
} else {
console.log('Senior');
}

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 }

Enter fullscreen mode Exit fullscreen mode

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;

}
Enter fullscreen mode Exit fullscreen mode

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!

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (1)

Collapse
 
olen_d profile image
Olen Daelhousen

Something that caught me by surprise is that variable scope is shared across all cases in a switch statement. This is not the case with if-else. For example if const foo = 'bar' is defined in the first switch case, const foo = 'cherry' is not valid in the second case. Using if-else, this would work as expected and the constant foo could be set in each if block because each block has its own scope.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay