The ternary operator is a conditional operator that evaluates a condition and returns one of two values based on whether the condition is true or false. It is a shorthand for if-else statements and is supported in many programming languages, though syntax varies.
JavaScript
In JavaScript, the ternary operator uses the syntax:
condition ? exprIfTrue : exprIfFalseExample:
const age = 26; const beverage = age >= 21 ? "Beer" : "Juice";It is right-associative, allowing chaining like
a ? b : c ? d : e, equivalent toa ? b : (c ? d : e).It is widely used for concise conditional assignments and is available across all modern browsers since July 2015.
C, C++, Java, and similar languages
These use the ?: syntax:
variable = condition ? expression1 : expression2;Only the selected expression is evaluated (short-circuit evaluation).
Example:
int max = (a > b) ? a : b;The operator has low precedence, so parentheses are often needed.
Python
Python uses a different, more readable syntax:
value_if_true if condition else value_if_falseExample:
status = "Adult" if age >= 18 else "Minor"It is officially called a conditional expression.
Unlike other languages, it does not use
? :syntax.Important: The tuple-based workaround
(if_false, if_true)[condition]is discouraged because both values are evaluated, which can cause errors (e.g.,1/0).
Key Benefits
Conciseness: Reduces code length for simple decisions.
Readability: Improves clarity for straightforward conditions.
Variable Initialization: Allows assignment during declaration, even for
constvariables.
Best Practices
Use for simple, clear conditions.
Avoid nesting multiple ternary operators to maintain readability.
Avoid side effects (e.g., function calls, assignments) in expressions, as they may lead to unpredictable behavior.
Note: While the ternary operator is powerful, complex logic should use
if-elsefor better maintainability.
What are pros/cons of ternary conditional operators? - Programming Language Design and Implementation Stack Exchange
c# - How does the ternary operator work? - Stack Overflow
Ternary Operator
Ternary conditional operator : golang
Videos
Pro: Readability
While plenty of people talk about the readability problems with misused ternary operators, there are also situations where using a full if/else would be annoyingly bulky, spreading a simple statement across five lines, and significantly reducing readability. Here are some sample statements pulled from a project I'm working on:
bg_2d.fillStyle = is_in_bounds(x, y) ? grid_colors[x][y] : "#ffffff";
const diagonal_slowdown = is_diagonal() ? Math.SQRT2 : 1;
const color = (team == "red") ? "#ff0000" : "#0000ff";
When you have a simple situation where you have two things to pick from based on a condition, which happens all the time, ternary is perfect. Having a short syntax for ternary massively improves readability in these situations.
Pros:
It's an expression, and in languages with distinction between expressions and statements it's crucial to have a selection operator available as an expression. Ternary operator is not necessarily the best syntax though, as the other answers highlighted how convoluted it can become with very little effort. A good alternative is an if-then-else available as an expression and not just statement.
Cons:
It's introducing control flow into expressions, just like logic operators in many languages. When side effects are possible, it's making expressions less visually obvious to follow.
The C-style syntax of C?T:F often leads to convoluted and error-prone code.
Boolean isValueBig = ( value > 100 ) ? true : false;
// above line is same as:
Boolean isValueBig;
if( value > 100 ) {
isValueBig = true;
} else {
isValueBig = false;
}
The difference between the ternary operation and if/else is that the ternary expression is a statement that evaluates to a value, while if/else is not.
To use your example, changing from the use of a ternary expression to if/else you could use this statement:
Boolean isValueBig = null;
if(value > 100)
{
isValueBig = true
}
else
{
isValueBig = false;
}
In this case, though, your statement is equivalent to this:
Boolean isValueBig = (value > 100);
What do people think about using the ternary '?' operator instead of ifelse?
Do you use it, how often, what are best practices concerning this way of writing conditional statements?