Skip to content

logical OR (||)

In JavaScript, the logical OR operator (||) returns its left operand when that value is truthy and its right operand otherwise. It short-circuits as soon as the left operand is truthy, so the right side never runs in that case.

The operator converts the left operand to a Boolean only to make that decision, using the same rules as Boolean(). It then returns the original value, so "guest" || "admin" evaluates to the string "guest". The falsy values are false, 0, -0, 0n, "", null, undefined, and NaN. Because || treats all of them as missing, a real 0 or "" gets replaced by the fallback.

JavaScript Logical OR Syntax

The logical OR operator joins two operands, and a chain of them reads from left to right:

Language: JavaScript Syntax
<left> || <right>
<first> || <second> || ... || <last>

A chain returns the first truthy operand, or the last operand when every operand is falsy.

JavaScript Logical OR Examples

Say a profile page shows a display name and falls back to a placeholder when the name is empty:

Language: JavaScript
> const profile = { name: "", city: "Lisbon" };
undefined

> profile.name || "Anonymous"
'Anonymous'

> profile.city || "Unknown"
'Lisbon'

> null || undefined || 0
0

The empty string is falsy, so || returns the placeholder. The truthy "Lisbon" comes back unchanged. When no operand is truthy, the chain returns the last one, here 0.

The same rule causes a common bug. A volume setting of 0 is a valid value, but || replaces it, while the nullish coalescing operator (??) keeps it:

Language: JavaScript
> const settings = { volume: 0 };
undefined

> settings.volume || 50
50

> settings.volume ?? 50
0

The right operand runs only when the left one is falsy, which skips a costly lookup when a cached value exists:

Language: JavaScript
> const loadLocale = () => { console.log("Loading locale"); return "en"; };
undefined

> "fr" || loadLocale()
'fr'

> "" || loadLocale()
Loading locale
'en'

JavaScript Logical OR Gotchas

  • The logical AND operator (&&) has higher precedence than ||, so a || b && c evaluates as a || (b && c).
  • Mixing || with ?? without parentheses is a SyntaxError, so a || b ?? c needs parentheses, such as (a || b) ?? c.
  • The result is always one of the operands, so "yes" || false evaluates to "yes". Wrapping the expression in Boolean() gives true or false.

JavaScript Logical OR Use Cases

  • Supplying a fallback when a value can be empty, zero, or missing
  • Skipping an expensive call when a cached value is already available
  • Combining several conditions in an if statement
  • Picking the first non-empty string from a list of candidates

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 12, 2026