Logical OR operator
Logical OR operator
The Logical OR operator (||
) is a JavaScript operator that evaluates two operands and returns the value of the first truthy operand.
It is typically used in conditional statements or assignments to provide fallback or default values. When the left operand is truthy, the operator returns the value of the left operand without evaluating the right operand.
If the left operand is falsy (e.g., false
, 0
, null
, undefined
, NaN
, or an empty string), the operator evaluates the right operand and returns its value.
This behavior makes the Logical OR operator useful for providing default values or handling optional values in JavaScript.
For example, const result = value || defaultValue;
assigns value
to result
if value
is truthy, but if value
is falsy, it assigns defaultValue
to result
.