JavaScript » Logical » ||


 
This is the logical OR operator and it returns a value of true if one or both of the operands is true. It works by first evaluating the left-hand operand and, if this is true, disregarding the right-hand one and returning true for the whole expression. If, however, the left-hand operand is false, then it returns the value of the right-hand operand: true or false if Boolean, or else the value itself.
 

 
...while this one returns 'cheese':
 

 
If, however, the first operand is not a Boolean value, the OR operator returns the value of the first operand whether the second is true or not; in the next example 'bread':
 

 

Examples

Code:
if((a == c) || (b == 9))
x = (a > b) || (c == 3)
Explanation:

These examples both return true.

Code:
x = (a > b) || "cheese"
Explanation:

This example returns 'cheese'.

Code:
x = "bread" || (c == 3)
Explanation:

If, however, the first operand is not a Boolean value, the OR operator returns the value of the first operand whether the second is true or not; in this next example 'bread'.