Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Logical OR operator ||

The || operator returns its left operand if it is truthy; otherwise, it evaluates and returns its right operand. It short-circuits and does not necessarily return a Boolean.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

left || right

left || right first evaluates left and checks its truthiness using ToBoolean. If it is truthy, the operator returns its original value, skipping right and its side effects. Otherwise, it evaluates and returns right, even if that value is also falsy.

Falsy values include false, undefined, null, 0, -0, 0n, NaN, and "". Other values are truthy, with the historical browser exception of document.all. Empty arrays and ordinary objects are truthy. The result of || is an operand value, not necessarily true or false.

Be careful with defaults: 0 || 10 produces 10. To replace only null and undefined, use ??: 0 ?? 10 produces 0. Combining || with ?? without parentheses is a syntax error. The && operator has higher precedence than ||; parentheses help express intent clearly. Do not confuse || with bitwise |.

Examples

let calls = 0;
function fallback() {
  calls += 1;
  return "guest";
}
console.log("Ada" || fallback());
console.log("" || fallback());
console.log(calls);
console.log(0 || 10);
console.log(0 ?? 10);

Result

Ada
guest
1
10
0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources