The logical OR (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
||) operator evaluates operands from left to right, returning the first truthy operand it encounters. If all operands evaluate to a falsy value, it returns the value of the final operand. Crucially, JavaScript’s || operator returns the actual uncoerced value of the operand, not necessarily a boolean.
Evaluation Mechanics
When evaluatingexpr1 || expr2, the JavaScript engine executes the following sequence:
- Evaluates
expr1. - Coerces the result of
expr1to a boolean to determine its truthiness. - If the coerced value is
true(truthy), the operator short-circuits. It immediately returns the uncoerced value ofexpr1, andexpr2is never evaluated. - If the coerced value is
false(falsy), the operator evaluatesexpr2and returns its uncoerced value.
Truthy vs. Falsy
The behavior of|| depends entirely on JavaScript’s definition of falsy values. An operand is falsy if its evaluated value is one of the following:
false0,-0, or0n(BigInt zero)"",'', or“ (empty strings)nullundefinedNaN
document.all (a legacy web API quirk defined in the ECMAScript specification Annex B that evaluates to falsy), every other value in JavaScript—including empty arrays ([]) and empty objects ({})—is truthy.
Evaluation Examples
Short-Circuiting Behavior
Because the operator stops evaluating as soon as it finds a truthy value, any expressions or side effects on the right-hand side of a truthy operand are completely ignored.Chaining
When chaining multiple|| operators, the engine evaluates sequentially from left to right, stopping at the exact moment a truthy value is resolved.
Operator Precedence
The|| operator has a lower precedence than the logical AND (&&) operator. In expressions containing both, && is evaluated first unless the order of operations is explicitly overridden using grouping parentheses ().
Contrast with Nullish Coalescing (??)
Because || short-circuits on any falsy value, it will bypass defined, non-nullish falsy values like 0, 0n, or "". To strictly check for nullish values (null or undefined) rather than all falsy values, modern JavaScript provides the Nullish Coalescing operator (??).
Master JavaScript with Deep Grasping Methodology!Learn More





