JavaScript Logical OR (||) Operator


Logical OR Operator

logical OR operator takes two operands, and returns true if any of the left or right operand is true, or false otherwise

|| symbol is used for logical OR operator

a || b

the following truth table provides the output of OR operator for possible operand values

aba || b
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Examples

1. check if num is positive or an even number

let num = 11;
if ((num > 0) || (num % 2 == 0)) {
    console.log('num is positive, even, or both');
} else {
    console.log('num is neither positive nor even');
}

in the above example, left operand a is (num > 0) and right operand b is (num % 2 == 0). for given value of num, a is true and b is false. if-condition is true, and therefore if-block is run