Skip to content
Advertisement

alternative approaches to multiple if else conditions [closed]

I have multiple conditions to be checked and executed like below.

JavaScript

I think using switch statement doesn’t make sense here since we are not evaluating case statement expression against switch expression.

So is it okay to go with multiple if else statements or any better alternative approach?

Advertisement

Answer

Your code is 100% a good option. It is just a bit hard to read. You can pull out common code into variable to make it more readable

JavaScript

Other option is to use ternary operators

JavaScript

It could be one big ternary, but it is a bit unreadable

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement