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 execute code when one of multiple conditions is satisfied.
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: We will use the OR operator on normal values in this example.
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.
function a() {
console.log("welcome")
return true;
}
function b() {
console.log("Hello")
return false;
}
console.log(a() || b());
Output
welcome true
Supported Browsers:
We have a complete list of JavaScript Logical Operators, to learn about those please go through JavaScript Logical Operators article.
Contact Us