Sub topic 5
How || Picks the First Truthy Operand
This chapter assumes you know the falsy values from earlier chapters, can read console.log output, and are comfortable with if statements and the ternary operator.
Many developers read || as "logical OR" and expect it to produce true or false. In JavaScript, || returns one of its operands instead. Knowing which operand it returns lets you predict the result of any || expression.
The Working Model
For an expression a || b, JavaScript follows these steps:
- Evaluate
a. - If
ais truthy, the result isa, exactly as it is. - If
ais falsy, the result isb, exactly as it is.
JavaScript tests the operand for truthiness, but it does not return that test's boolean. It returns the original value. If the left operand is the string "a", the result is the string "a", not true.
Seeing It in Code
console.log("a" || "fallback"); // "a"
console.log("" || "fallback"); // "fallback"
console.log(null || 0); // 0
console.log(0 || null); // null
console.log(42 || false); // 42
console.log(false || undefined); // undefinedLook at null || 0. Both operands are falsy, yet the result is 0, not false. When the left side is falsy, || returns the right side without testing it. So if every operand is falsy, you get the last one.
Chains of ||
|| is left-associative, which means a || b || c is read as (a || b) || c. In practice, the chain returns the first truthy operand it finds. If it finds none, it returns the last operand.
const fromArgs = undefined;
const fromEnv = "";
const fromConfig = "8080";
const port = fromArgs || fromEnv || fromConfig || "3000";
console.log(port); // "8080"
console.log(0 || "" || null); // null (no truthy operand, so the last one)Once the chain finds a truthy value, it stops evaluating the rest. The chapter on short-circuit evaluation covers what that means for function calls and side effects.
Inside an if Statement
You don't notice the returned value when || sits in a boolean context, because the if converts the result to a boolean anyway:
const name = "";
const nickname = "Sam";
if (name || nickname) {
console.log("Has some name"); // logs, because "Sam" is truthy
}
const display = name || nickname;
console.log(display); // "Sam", a string, not trueThe same expression gives a boolean-like effect in the if and a string when you assign it. The value only becomes true or false when something converts it.
Common Errors
| Mistake | What actually happens | Column | Column |
|---|---|---|---|
| Expecting `x | y` to be a boolean | It is `x` or `y`, of whatever type they are | |
| Comparing `(a | b) === true` | Fails unless the chosen operand is literally `true` | |
| Assuming all-falsy gives `false` | It gives the last operand, such as `null` or `0` | ||
| Treating `0` or `""` as missing | ` | ` skips them because they are falsy |
Exercises
Exercise
What does console.log(undefined || NaN || "") print, and why?
Show answer
It prints an empty string (the console shows a blank line). All three operands are falsy, so || never finds a truthy value and returns the last operand, "".
Exercise
Given const result = [] || "empty";, what is result, and what is its type?
Show answer
result is the empty array [], type object. An empty array is truthy, so || returns it unchanged and never looks at "empty".
Exercise
Why does if ((0 || "yes") === true) not run its body?
Show answer
0 || "yes" evaluates to the string "yes", not the boolean true. Strict equality between "yes" and true is false, so the body is skipped.