Tuesday, October 8, 2024
Google search engine
HomeLanguagesJavascriptOR(||) Logical Operator in JavaScript

OR(||) Logical Operator in JavaScript

JavaScript logical OR(||) Operator or Logical disjunction Operator operates on a set of operands and returns true even if one of the operands is true. It evaluates the operands from left to right and returns true whenever it encounters the first truthy value otherwise false.

The Logical OR Operator can be used on non-boolean values but it has a lower precedence than the AND operator.

Syntax:

a||b

Example 1: In this example, we will use the OR operator on normal values.

Javascript




console.log(true || false);
console.log(false || false);
console.log(1 || 0);
console.log(1 || 2);
console.log("1" || true);
console.log("0" || true);


Output:

true
false
1
1
1
0

Example 2: In this example, we will use the OR operator on function calls.

Javascript




function a() {
    console.log("welcome")
    return true;
}
function b() {
    console.log("Hello")
    return false;
}
console.log(a() || b());


Output: We see that Hello is not printed on the console as when the first truthy value is encountered the OR operator is evaluated and returns true so the function b() is never called.

welcome
true

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Safari
  • Opera

We have a complete list of JavaScript Logical Operators, to learn about those please go through JavaScript Logical Operators article.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments