Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The Logical OR Operation
3.
Usage of Javascript OR Operator
4.
Implementation
4.1.
JavaScript
5.
Short Circuit Evaluation
6.
Converting OR to Other Logical Operators
6.1.
Converting OR to AND
6.2.
Converting Or to Ternary Operator
7.
Frequently Asked Questions
7.1.
How is the OR operator used in function parameters?
7.2.
What is the difference between the OR operator and the AND operator (&&) in JavaScript?
7.3.
How can the OR operator be used to check for the existence of a variable?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

OR(||) Logical Operator in JavaScript

Author Ravi Khorwal
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Javascript is one of the most popular programming languages for developing web applications. Operators are a fundamental part of this language. They help us to perform some logical operations on data. 

In this article, we will learn about the Javascript OR operator. It is an important logical operator that helps us to perform the OR operation on a data set. It is represented by the symbol ‘||’ (two parallel lines).

Let us start by understanding the definition of the javascript OR operator.

OR(||) Logical Operator in JavaScript

Recommended Topic, Javascript hasOwnProperty

The Logical OR Operation

The Javascript OR operator considers a set of operands and returns true if and only if one or more of these operands are true. This operation is generally used with boolean values but works fine with non-boolean values as well. 

Syntax:

<variable_name> = (condition1 || condition2 || condition3 … || conditionN).

 

Consider the simple examples below.

For boolean values

Code: 

let a = true;
let b = false;
let c = a || b;  // c will be assigned the value "true"

 

Here, the variable will be assigned the value true because one of its arguments, a, is true.

For non-boolean

Code:

let a = false;
let b = “coding ninjas”;
let name = a || b;

 

In this case, the variable name will be assigned the value “coding ninjas.”

Note that the javascript OR operator treats some expressions as false. 

These are

  • null;
  • NaN (Not a Number);
  • 0;
  • empty string (" " or ' ' or ` `);
  • Undefined.
     

Note that the above-mentioned values are called falsy. This means that the OR operator evaluates these values as false. All the other types of values which are not falsy are called truth.

Read More About, Basics of Javascript

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

 

  • 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
}

 

  • 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"

 

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

 

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. 

  • JavaScript

JavaScript

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

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

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

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

// 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:

Output

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

 

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

 

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

 

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;

 

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"

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

 

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

 

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 testssolve 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! 💻

Previous article
Setattribute Javascript
Next article
Math Random Javascript
Live masterclass