Skip to main content

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.

The logical OR (||) 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.
expr1 || expr2

Evaluation Mechanics

When evaluating expr1 || expr2, the JavaScript engine executes the following sequence:
  1. Evaluates expr1.
  2. Coerces the result of expr1 to a boolean to determine its truthiness.
  3. If the coerced value is true (truthy), the operator short-circuits. It immediately returns the uncoerced value of expr1, and expr2 is never evaluated.
  4. If the coerced value is false (falsy), the operator evaluates expr2 and 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:
  • false
  • 0, -0, or 0n (BigInt zero)
  • "", '', or “ (empty strings)
  • null
  • undefined
  • NaN
With the exception of 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

// Returns the first truthy value
"hello" || "world"   // Returns "hello" (string)
0 || 42              // Returns 42 (number)
null || true         // Returns true (boolean)

// Returns the last value if all are falsy
false || 0           // Returns 0
undefined || null    // Returns null
"" || NaN            // Returns NaN

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.
// No ReferenceError is thrown because the right side is never reached
const a = true || someUndeclaredVariable; 

let x = 0;
const b = "truthy" || (x = 5); 
// b is "truthy". x remains 0 because the assignment is never evaluated.

Chaining

When chaining multiple || operators, the engine evaluates sequentially from left to right, stopping at the exact moment a truthy value is resolved.
const result = null || undefined || "first truthy" || "ignored";
// result is "first truthy"

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 ().
// Evaluated as: true || (false && false)
true || false && false; // Returns true

// Evaluated as: (true || false) && false
(true || false) && false; // Returns false

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 (??).
// || bypasses the falsy 0, returning the right-hand operand
0 || 42; // Returns 42

// ?? only bypasses null or undefined, returning the left-hand operand
0 ?? 42; // Returns 0
Master JavaScript with Deep Grasping Methodology!Learn More