Alternative to Multiple OR Operators in JavaScript - GeeksforGeeks

20 Oct, 2021
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.
The below JavaScript code could be rewritten to perform the same task in the 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 GeeksforGeeks */if (user == "geek" ||    user == "motivated" ||    user == "curious") {  recommended_org = "GeeksforGeeks";  console.log(recommended_org);}
Output: 
GeeksforGeeks
 
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. 
The example below demonstrates this approach:
javascript
/* If user is a geek or motivated or    curious then the recommended organisation    for him/her is GeeksforGeeks */if (user == "geek") {  recommended_org = "GeeksforGeeks";}else if (user == "motivated") {  recommended_org = "GeeksforGeeks";}else if (user == "curious") {  recommended_org = "GeeksforGeeks";}else {  recommended_org = "No recommendation"}console.log(recommended_org);
 Output: 
GeeksforGeeks 
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 make 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 that flexible as if-else. 
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 GeeksforGeeks */switch (user) {  case "geek":  case "motivated":  case "curious":    recommended_org = "GeeksforGeeks"    break;  default:    recommended_org = "No Recommendation"} console.log(recommended_org);
 Output: 
GeeksforGeeks
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. 
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": "GeeksforGeeks",    "motivated": "GeeksforGeeks",    "curious": "GeeksforGeeks",    "other": "No Recommendation"} /* if user is a geek or motivated or   curious then the recommended organisation   for him/her is GeeksforGeeks */console.log(recommended_org[user] ||  recommended_org["other"]);
Output:
GeeksforGeeks 
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. 
surindertarika1234
JavaScript-Misc
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to Open URL in New Tab using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills