Logical OR (||)
Logical OR: returns the first truthy value.
Available since: ES1
Syntax
a || bExamples
In conditions — at least one is true:
const age = 70;
console.log(age < 18 || age > 65);Output:
trueIt returns the value itself, not just true or false:
console.log('' || 'default');
console.log('Anna' || 'default');
console.log(0 || null);Output:
default
Anna
nullNotes
If the left side is truthy the right side is not evaluated at all. For defaults where 0 or an empty string are valid, use ??.
See also
- Logical AND (&&) — Logical AND: returns the first falsy value, or the last one.
- Nullish coalescing (??) — Returns the right-hand value if the left is null or undefined.
- Boolean() — Converts a value to true or false.
In the tutorial
- Logical operators — JavaScript logical operators — AND, OR, NOT, their precedence, default values with || and the ?? operator.