Conditional operator#
The conditional (ternary) operator selects between two values based on a condition.
Syntax#
The operator has the form condition ? true_value : false_value.
Type requirements#
The condition must evaluate to a boolean. Both value branches must have the same type.
Short-circuit evaluation#
Only the selected branch is evaluated.
Nesting#
Ternary expressions can be nested. Use parentheses or newlines for clarity.
// input: x = 33, target = 42
x > target ? "too big" :
x < target ? "too small" : "just right!"
// result: "too small" (string)
See also#
- Logical operators - Boolean operations
- Comparison operators - Building conditions