In this article, we will learn the alternatives to the use of multiple || (OR operator) in conditional statements. Multiple OR operators in conditional statements might cause poor readability of code and are often limited in functionality.
Example: The below JavaScript code could be rewritten to perform the same task in other ways:
javascript
let user = "motivated" ; let recommended_org = "" ; /* If user is a geek or motivated or curious then the recommended organisation for him/her is neveropen */ if (user == "geek" || user == "motivated" || user == "curious" ) { recommended_org = "neveropen" ; console.log(recommended_org); } |
Output:
neveropen
Approach 1: This approach uses an if-else ladder to handle all the different possibilities.
In this approach, the syntax is heavily increased. As the outcome of each, if-statement is the same, it may be preferable to use the original method of using multiple OR operations.
However, as the program becomes more complex in options, the if-else ladder is preferred as it increases the readability of the code. It is also very flexible as one can add any number of conditions and even nested conditions, Due to such reasons, it may be used in complex conditions.
Example: The example below demonstrates this approach:
javascript
<script type= "text/javascript" charset= "utf-8" > /* If user is a geek or motivated or curious then the recommended organisation for him/her is neveropen */ let user = "geek" if (user == "geek" ) { recommended_org = "neveropen" ; } else if (user == "motivated" ) { recommended_org = "neveropen" ; } else if (user == "curious" ) { recommended_org = "neveropen" ; } else { recommended_org = "No recommendation" } console.log(recommended_org); </script> |
Output:
neveropen
Approach 2: This approach uses switch-case statements to handle all the different possibilities.
Switch-case statements are powerful they increase the readability of the code and also the use of default and break statements makes the code more efficient to write.
The syntax is increased in this case by the use of switch-case statements but there is less repetition of code as compared to the previous approach. In the if-else ladder, we used the assignment statement three times but in this approach, we use it a single time. But nesting switch case will make the code very complex and it is generally not preferred for problems having a lot of nested conditionals so it’s not as flexible as if-else.
Example: The example below demonstrates this approach:
javascript
let user = "curious" ; let recommended_org = "No recommendation" ; /* If user is a geek or motivated or curious then the recommended organisation for him/her is neveropen */ switch (user) { case "geek" : case "motivated" : case "curious" : recommended_org = "neveropen" break ; default : recommended_org = "No Recommendation" } console.log(recommended_org); |
Output:
neveropen
Approach 3: This approach uses a map object to handle all the different possibilities.
This approach is very flexible like the if-else ladder and easy to scale as one needs to add more key-value pairs, but still, it cannot replace the above approaches in case of very complex examples.
Example: The example below demonstrates this approach:
javascript
let user = "geek" ; /* A recommended_org map object which consists the behaviour of the user as the key and the recommended organisation as the values */ var recommended_org = { "geek" : "neveropen" , "motivated" : "neveropen" , "curious" : "neveropen" , "other" : "No Recommendation" } /* if user is a geek or motivated or curious then the recommended organisation for him/her is neveropen */ console.log(recommended_org[user] || recommended_org[ "other" ]); |
Output:
neveropen
Any of the alternatives cannot be termed as superior as all have some pros and cons. It depends on the personal opinion of the programmer and the context where it is to be used. However, it is still a good practice to know about the available alternatives.