The ternary conditional (also known as the ternary operator) is a control structure that allows evaluating a condition and returning a value based on that condition.
Unlike the IF-ELSE conditionals, the ternary conditional consists of a single line of code and has the following general syntax:
result = condition ? value_if_true : value_if_false
In this expression:
- The condition is evaluated
- If it is
true
,value_true
is returned - If it is
false
,value_false
is returned
This structure is very useful when a value needs to be assigned to a variable based on a condition.
Just like in other conditionals, condition
must be an expression that evaluates to a boolean value. That is, it must be one of the following expressions,
false
ortrue
- A boolean variable
- An expression that evaluates as boolean
- A function call that returns a boolean
The ternary operator is called so because, in general, it is the only operator in a language that has three parameters. The rest usually have one parameter, like the negation operator !myvariable
or two, like the addition operators a+b
Equivalent in IF-ELSE
If we make the equivalent in an IF-ELSE block of the ternary operator, it would look something like this.
let result
if(condition) result = value_if_true
else result = value_if_false
As we see, it is (slightly) longer.
Examples of ternary operator in different languages
As I said, many languages implement the ternary operator as a means of comparison.
For example, in the case of C, C++, Java, and C#, the ternary conditional takes the following form,
int age = 18;
string result = age >= 18 ? "You are of legal age" : "You are underage";
In this example, we use the ternary conditional in C# to assign a message according to the age. If the age is greater than or equal to 18, the message “You are of legal age” is assigned; otherwise, the message “You are underage” is assigned.
In JavaScript, the ternary operator is identical. The only difference is that JavaScript does not declare the type of the variables, but the syntax of the conditional is the same.
let age = 18;
let result = age >= 18 ? "You are of legal age" : "You are underage";
In Python, the conditional is somewhat different, and has the following form.
age = 18
result = "You are of legal age" if age >= 18 else "You are underage"
To give a more bizarre example, in SQL the ternary operator does not exist, and instead, a function IFF
is provided that serves the same purpose.
IIF(@type = 2, 1, 0)
Nesting ternary operators
Just like the rest of the operators, the ternary conditional can be chained. This allows for generating more complex conditionals, such as the following.
let condition1 = false;
let condition2 = true;
let b = condition1 ? 'true1'
: condition2 ? 'true2'
: 'false_both';
In some circumstances, this can improve code readability. However, it is advisable not to overuse it because, in general, you can create statements that are very complicated to read.
Best practices Tips
Keep it simple and readable: The ternary operator can be useful for expressing conditional logic concisely. However, avoid overusing it and ensure that the expression is easy to understand and not too complex.
Avoid nesting ternary operators: Nesting multiple ternary operators can hinder code readability. In general, avoid it.
Do not make heavy function calls: The ternary operator should always be simple. Do not use it for
var a = validateData() ? value_true : value_false;
Instead, prefer
var isValid = validateData()
var a = isValid ? value_true : value_false;
Prefer clarity over brevity: When in doubt about whether the code is more readable or less, consider using IF-ELSE conditionals or breaking the logic into multiple lines to make it clearer and more understandable.
Use parentheses to avoid ambiguities: If you have more complex expressions involving ternary operators, use parentheses to ensure that the evaluation occurs correctly and to avoid ambiguities in operator precedence.
Relationship with functional paradigm Advanced
It is often said that the ternary conditional follows the functional programming paradigm. Well, that is not exactly true. Let’s say that it is “more inclined” towards functional programming than an IF. But whether it is functional or not totally depends on how we use it.
For example, if we use it as usual, evaluating a condition and returning one value or another, it is functional programming.
let result = condition ? 'yes' : 'no'
If instead of returning a variable or a literal, I make a function call, we start to approach the red zone.
function function_yes() {
// do nothing
return 'yes'
}
function function_no() {
// do nothing
return 'no'
}
let result = condition ? function_yes() : function_no();
In the previous example, the functions return values but do not perform actions. It is still functional programming, but less so than before.
If in this example, instead of returning a value, we performed actions, it would no longer be functional. For example, the following,
function function_yes() {
// do things and return nothing
}
function function_no() {
// do things and return nothing
}
condition ? function_yes() : function_no();
That is not functional at all (no matter how much you are using a ternary conditional). Here the ternary operator is being used to create a code branch. We are not even using the resolved value.
I’m not saying it’s incorrect (I don’t find it clean, but that’s debatable). What it certainly is not is functional.
So if you ever have a conversation about this, the correct way to say it is “the ternary operator facilitates a functional paradigm,” but it is not functional. It will depend on how we use it.