Alternative to Multiple OR Operators in JavaScript

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

/* 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);

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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);

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

let user = "geek";
  
/* A recommended_org map object whic 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"]);

chevron_right


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.

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.