Here, you’ll learn what this function does and how you can use it in your programming.
A ternary operator evaluates conditions to present a true or false value, but with more concise syntax and parameters than other methods.
The syntax of a ternary operator is as follows:
condition ? exprIfTrue : exprIfFalse
The condition is what your code will evaluate to decide the correct output. The exprIfTrue is the expression you will get if the condition is evaluated as true. The exprIfFalse is the expression you will get if the condition is evaluated as false.
Here is an example of an if-else statement:
Let’s say someone takes the test and gets a score of 2. In this instance, the output will be “You have failed the test.”
As you can see from this example, if-else statements are very clear and easy to interpret, but they require multiple lines of code.
Here’s how the if-else example above looks when written as a ternary operator instead:
The (?) represents the endpoint of the conditions the ternary operator should check. In this instance, the operator is being asked to check whether the result is greater than 5. This is the first operand.
Everything after the (?) represents the expressions available as results. The second operand is the exprIfTrue, or "You have passed the test." The third operand is the exprIfFalse, or "You have failed the test."
The end result is the same as the if-else statement, but the amount of code is vastly reduced.
Many situations require more complex operators than the first example above. But you can easily use a ternary operator for more complicated scenarios with multi-line expressions.
Let’s compare an if-else and a ternary operator statement again to demonstrate using ternary operators for multi-line expressions.
Take this statement for example:
While this is easy to interpret, there’s a lot going on. Here is what the same statement looks like using a ternary operator:
It’s as simple as using brackets to hold the expressions and separating them with commas.
So, why wouldn’t you use a ternary operator in these situations all the time?
It really comes down to how easy it needs to be to interpret your code. If you’re working on large teams or need to hand a project off to other developers, ternary operators can sometimes get a little confusing. That’s why, even though they’re shorter and more simple to write, most people stick with an if-else statement for anything other than a single-line expression.
Using One Ternary Operator Inside Another
Placing one ternary operator inside another is also known as a nested ternary operator. In the examples above, we were using ternary statements to determine whether someone had passed or failed a test.
But what if you’re dealing with multiple grades and scoring brackets? That’s where a nested ternary operator can be useful.
Here is how it would look:
Each subsequent ? represents its own ternary operator within the overall operator represented by score >= 85 ? "A".
Once again, it’s efficient. But it’s not that easy to interpret at a glance. Here is how the same statement would look as an if-else:
There’s no doubt that this is a much longer way to write the statement. But colleagues and anyone else reading your code will be able to understand and amend it more quickly.
Working With Ternary Operators
But sometimes, if-else statements create an unnecessary amount of writing. Ternary operators can be used in cases where it would be just as simple to interpret as an if-else statement. When two statements can be used interchangeably like this, it’s up to your best judgment which is preferable. Keeping a balance between ease of interpretation and code space in mind will generally lead you to the right one.