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.