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:
<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:
> 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:
> 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:
> 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||, soa || b && cevaluates asa || (b && c). - Mixing
||with??without parentheses is aSyntaxError, soa || b ?? cneeds parentheses, such as(a || b) ?? c. - The result is always one of the operands, so
"yes" || falseevaluates to"yes". Wrapping the expression inBoolean()givestrueorfalse.
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
ifstatement - 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