Usage of Javascript OR Operator
Let us look at the important and useful applications of the javascript OR operator.
-
Assigning default values: It is commonly used in default value assignments. If the left side of the operator is truthy, then it will be assigned. Otherwise, the right side will be assigned.
let name = userName || "Guest";

You can also try this code with Online Javascript Compiler
Run Code
- Short-circuit evaluation: The OR operator can be used in a condition where the operator's right-hand side is only evaluated if the left-hand side is falsy. We will discuss more about this feature in later sections.
-
Combining conditions: The OR operator can be used to combine multiple conditions in a single statement.
if (age < 18 || age > 65) {
// Do something
}

You can also try this code with Online Javascript Compiler
Run Code
-
In ternary operator: The OR operator can also be used in ternary operator (conditional operator) where it will check the left operand first, and then it will check the right operand.
let result = x > 0 ? "Positive" : x < 0 ? "Negative" : "Zero"

You can also try this code with Online Javascript Compiler
Run Code
-
In bitwise OR operator: The OR operator can also be used as a bitwise operator. It compares each bit of the first operand to the corresponding bit in the second operand. If either bit is 1, the corresponding result bit is set to 1.
let z = x | y;

You can also try this code with Online Javascript Compiler
Run Code
These are just a few examples of how the javascript OR operator can be used. It's a versatile operator that can be used in many different ways to help simplify and streamline your code.
Implementation
Let us now consider an example code, which will demonstrate all the above-mentioned features of the OR operator in javascript. We suggest you to give a proper read to the comments in the code. These will help you understand the implementation in a better way.
// Assigning default values
let userName = "John";
let name = userName || "Guest"; // if userName is defined, its value will be assigned to name. If userName is undefined, the string "Guest" will be assigned to name.
console.log(name); // Output: "John"
// Short-circuit evaluation
let x;
let y = 10;
x = y || (x = 5); // if y is truthy, the x = 5 will not be executed. If y is falsy, x = 5 will be executed and 5 will be assigned to x
console.log(x); // Output: 10
// Combining conditions
let age = 25;
let message = (age >= 21) ? "Welcome!" : "Sorry, you're not old enough"; // if age is greater than or equal to 21, the message "Welcome!" is assigned to the message. If not, the message "Sorry, you're not old enough" is assigned.
console.log(message); // Output: "Welcome!"
// In ternary operator
let a = -5;
let b = 20;
let result = a > 0 ? "Positive" : b > 0 ? "Positive" : "Zero"; // if a > 0 is true, it will return "Positive" otherwise it will check b > 0 if true it will return "Positive" otherwise it will return "Zero"
console.log(result); // Output: "Positive"
// In bitwise OR operator
let p = 60;
let q = 13;
let r = 0;
r = p | q; // compares each bit of p to the corresponding bit of q. If either bit is 1, the corresponding result bit is set to 1.
console.log(r); // Output: 61

You can also try this code with Online Javascript Compiler
Run Code
Output:
John
10
Welcome!
Positive
61

You can also try this code with Online Javascript Compiler
Run Code
Short Circuit Evaluation
In JavaScript, the OR operator (||) uses short-circuit evaluation. This means that the right-hand side of the operator is only evaluated if the left-hand side is falsy. This is because the OR operator returns the first truthy value it encounters.
Here's an example:
let x = y || doSomething();

You can also try this code with Online Javascript Compiler
Run Code
In this case, if y is truthy, the doSomething() function will not be called. If y is falsy, the doSomething() function will be called, and its return value will be assigned to x.
Short-circuit evaluation can be useful in situations where you want to assign a default value to a variable but also want to avoid unnecessary function calls. For example, if you want to assign a default value to a variable only if the variable is undefined, you can use the OR operator like this:
let name = userName || "Guest";

You can also try this code with Online Javascript Compiler
Run Code
Here, if userName is defined, its value will be assigned to name. If userName is undefined, the string "Guest" will be assigned to name instead.
Short-circuit evaluation can also be used to execute a function conditionally. For example, if you have a function that should only be executed if a certain condition is met, you can use the OR operator like this:
let x = checkCondition() || doSomething();

You can also try this code with Online Javascript Compiler
Run Code
In this case, if checkCondition() returns a truthy value, doSomething() will not be called. If checkCondition() returns a false value, doSomething() will get executed.
Short-circuit evaluation can also be useful when you want to assign a value to a variable only if it is currently undefined. For example, you can use the OR operator like this:
let x = x || 5;

You can also try this code with Online Javascript Compiler
Run Code
This will assign 5 to x if x is currently undefined or false.
Read about Bitwise Operators in C here.
Converting OR to Other Logical Operators
The javascript OR operator (||) can be converted to other logical operators, such as the logical AND operator (&&) and the ternary operator (? :) using logical equivalences.
Converting OR to AND
Here's an example of how you can convert the OR operator to the AND operator:
Code:
let a = true;
let b = false;
let c = a || b; // c will be assigned the value "true"
// equivalent to
let c = !(!a && !b); // c will also be assigned the value "true"

You can also try this code with Online Javascript Compiler
Run Code
Converting Or to Ternary Operator
Here's an example of how you can convert the OR operator to the ternary operator:
Code:
let x = y || doSomething();
// equivalent to:
let x = y ? y : doSomething();

You can also try this code with Online Javascript Compiler
Run Code
Here's an example of how you can convert the OR operator to the ternary operator with a default value:
Code:
let name = userName || "Guest";
// equivalent to:
let name = userName ? userName : "Guest";

You can also try this code with Online Javascript Compiler
Run Code
It's important to note that these conversions may not always be equivalent and can lead to different behavior or results in certain situations. It's also important to note that not all the cases of the OR operator can be converted to other logical operators. Hence, we suggest you test your code after making such changes to ensure it still behaves as expected.
Must Read Fibonacci Series in JavaScript
Frequently Asked Questions
How is the OR operator used in function parameters?
The OR operator is often used in function parameters as a way to provide a default value for an optional parameter. This way, if the parameter is not passed when the function is called, the default value will be used instead.
What is the difference between the OR operator and the AND operator (&&) in JavaScript?
The OR operator (||) and the AND operator (&&) in JavaScript are both logical operators. The OR operator returns the first truthy value of the expressions on either side of it, while the AND operator returns the first falsy value of the expressions on either side of it.
How can the OR operator be used to check for the existence of a variable?
The OR operator can be used to check for the existence of a variable by comparing it to undefined or null. For example, if (variable || variable === undefined) will return true if the variable exists and is not undefined or null.
Conclusion
In this article, we discussed the javascript OR operator. We learned how we can use this operator for assigning default values to variables and various other logical operations. We also understood the concept of short-circuiting and how we can use it to our advantage.
We hope you liked reading this blog. If you wish to learn more about javascript, do visit these articles
Visit our website to read more such blogs. Make sure you enroll in our other courses as well. You can take mock tests, solve problems, and interview puzzles. Also, you can check out some exciting interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.
Keep Grinding! 🦾
Happy Coding! 💻