Programming Fundamentals › Programming Basics
Ternary Operator
A compact inline if/else expression.
Also known as: conditional operator, inline if, ternary expression
The ternary operator is a compact inline if/else that produces a value. It takes three parts: a condition, a result if true, and a result if false.
const label = age >= 18 ? "adult" : "minor";
label = "adult" if age >= 18 else "minor"
Because it’s an expression, you can use it anywhere a value goes: assignments, arguments, template strings, JSX.
<p>{items.length ? `${items.length} items` : "Empty"}</p>
When it helps
Choosing between two simple values, with no side effects.
When it hurts
Nesting makes it unreadable:
const x = a ? b ? 1 : 2 : c ? 3 : 4; // don't
Use if/else, an early return or a lookup table when there are more than two outcomes, or when the branches do actions rather than produce values (conditionals, switch).
Gotchas
- Both branches should return compatible types, or the result type becomes a union.
- It evaluates only the chosen branch, so it’s safe for guards (
user ? user.name : "guest"). - For a default when a value is missing,
??(JavaScript) oror(Python) is shorter, but mind the truthy rules. - Python’s order is unusual: value-if-true comes first, then the condition.
Rule of thumb: if you have to read it twice, write it as an if.