Sub topic 6
How && Picks the First Falsy Operand
This chapter assumes you know the falsy values, you can read console.log output, and you have seen in the previous chapter how || returns one of its operands. The && operator follows the same idea in the opposite direction.
The Rule
The && operator does not always produce true or false. It evaluates its operands from left to right and returns one of them:
- If an operand is falsy,
&&returns that operand and stops. - If every operand is truthy,
&&returns the last one.
Put simply, && looks for the first falsy value. If it finds none, the result is whatever came last.
console.log("a" && "b"); // "b"
console.log(0 && "b"); // 0
console.log("" && "x"); // ""
console.log(null && undefined); // null
console.log(1 && "x" && true); // true
console.log("a" && 0 && "c"); // 0In "a" && "b", both operands are truthy, so the last one, "b", comes back. In 0 && "b", the first operand is falsy, so 0 itself comes back. You get the number 0, not the boolean false.
Chains of &&
A chain like a && b && c groups as (a && b) && c, but the effect is the same as a left-to-right scan:
The last operand is never tested. It is returned whatever its value, which is why "a" && "b" && 0 returns 0 and "a" && "b" && "c" returns "c".
| Expression | Result | Reason |
|---|---|---|
| `true && "yes"` | `"yes"` | All truthy, so the last operand is returned |
| `NaN && "yes"` | `NaN` | `NaN` is the first falsy operand |
| `[] && {}` | `{}` | Both are truthy objects |
| `"ok" && null && 5` | `null` | `null` is the first falsy operand |
Comparing && with ||
The two operators mirror each other:
| Operator | Stops at | Returns if it never stops | Column | Column |
|---|---|---|---|---|
| `\ | \ | ` | first truthy operand | last operand |
| `&&` | first falsy operand | last operand |
Why This Matters in Real Code
Inside an if condition the returned operand is converted to a boolean, so you rarely notice the difference. It shows up when you store or display the result:
const user = null;
const name = user && user.name;
console.log(name); // null
const admin = { name: "Ada" };
const adminName = admin && admin.name;
console.log(adminName); // "Ada"The user && user.name pattern reads a property only when the object exists. If user is falsy, the expression returns user itself (here null), and user.name is never evaluated. That skipping behavior is short-circuit evaluation, which the next chapter covers in detail.
Common Errors
- Expecting
falsewhen you get0,""ornull.&&returns the falsy operand itself. - Assuming the last operand gets checked. It is returned as-is, even when it is falsy.
- Comparing the result with
===.(x && y) === trueonly holds whenyis literallytrue.
Exercise
What does [] && "" && 0 return, and why?
Show answer
It returns "" (an empty string). The empty array [] is truthy, so evaluation continues. The empty string "" is the first falsy operand, so && returns it and never reaches 0.
Exercise
Given const count = 3, what is the value of count && count * 2 && "done"? What does it become when count is 0?
Show answer
With count = 3 it returns "done", because 3 and 6 are both truthy and "done" is the last operand. With count = 0 it returns 0, the first falsy operand, and the rest of the chain is skipped.