Alternative to Multiple OR Operators in JavaScript
Last Updated :
17 Jan, 2023
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 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.
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 GeeksforGeeks */
let user ="geek"
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);
</script>
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 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 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.
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": "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.
Similar Reads
OR(||) Logical Operator in JavaScript
The logical OR (||) (logical disjunction) operator for a set of operands is true if and only if one or more of its operands is true. It evaluates two expressions and returns true if at least one is true, otherwise, it returns false. This operator is frequently used in conditional statements to execu
1 min read
OR(|) Bitwise Operator in JavaScript
JavaScript Bitwise OR(|) Operator is used to compare two operands by performing OR operation on individual bits of the operands and returns true even if one of the compared bits is 1. The OR Operator has vast applications and the most used one is combining bit values. The operation is represented by
2 min read
AND(&&) Logical Operator in JavaScript
The logical AND (&&) (logical conjunction) operator for a set of boolean operands will be true if and only if all the operands are true. Otherwise, it will be false. It's commonly used to combine conditions in conditional statements or to check multiple conditions before executing code. The
1 min read
Less Than or Equal(<=) Comparison Operator in JavaScript
JavaScript Less Than or Equal(<=) to the operator is used to compare two operands and return true if the left operand is smaller or equal to the right operand. The algorithm used for the comparison is the same as that of less than operator but equal condition is also checked Syntax: a<=b Examp
1 min read
XOR(^) Bitwise Operator in JavaScript
In JavaScript, the bitwise XOR(^) Operator is used to compare two operands and return a new binary number which is 1 if both the bits in operators are different and 0 if both the bits in operands are the same. The operation is represented by the "^" symbol. This operator can be used to find the miss
2 min read
Bitwise OR Assignment (|=) Operator in JavaScript
The Bitwise OR Assignment Operator in JavaScript is represented by (|=). This operator is used to perform a bitwise OR operation on both operands and assign the result to the left operands. Syntax: a |= b Where - a = First operandb = Second operand Example 1: In this example, we will use basic numer
2 min read
JavaScript Logical OR assignment (||=) Operator
This operator is represented by x ||= y and it is called a logical OR assignment operator. If the value of x is falsy then the value of y will be assigned to x. When we divide it into two parts it becomes x || ( x = y ). It checks if x is true or false, if the value of x is falsy then it runs the (
2 min read
How to concatenate regex literals in JavaScript ?
Regex is a sequence of pattern that is used for matching with a pattern. While searching for data in a text, the search pattern is described for what we are searching for. It can be a single character or a more complex pattern. It can be used to perform all types of text searches. Regex has its own
2 min read
Left Shift (<<) Bitwise Operator in JavaScript
JavaScript Left Shift Operator is used to operate on the two operands. The first operand is the number and the right operand specifies the number of bits to shift to the left. The operation is represented by the "<<" symbol. Mainly the left shift operator is used to multiply the number by any
2 min read
Javascript Short Circuiting Operators
In JavaScript, short-circuiting refers to the process of evaluating expressions from left to right and stopping as soon as the result is determined. If the outcome of an expression is clear before evaluating all conditions, the evaluation is âshort-circuited,â meaning the remaining conditions are ig
2 min read