There are several conditional operators in javascript (if...else, switch). In this article, we will discuss the ternary operator and use of the ternary operator.
Table of Contents
What is The Ternary Operator?
- The ternary operator is a conditional operator that evaluates the expression and returns
trueorfalse. - The ternary operator determines which expression to execute based on whether the condition evaluates as true or false.
- The syntax of a ternary operator is as follows:
conditionalExpression ? firstExpression : secondExpression- If the returned value from
conditionalExpressionis true, the ternary operator will executefirstExpression. - If the returned value from
conditionalExpressionis false, the ternary operator will executesecondExpression. - When ternary operators return a value, we can store returning value in a variable:
const finalValue = conditionalExpression ? firstExpression : secondExpressionHow to Use The Ternary Operator in Place of if Statement
- We can replace the
ifstatement with a ternary operator. - Ternary operator makes our code short, clean, and readable. For example:
const percentage = 60
let resultStatus
if (percentage > 33) {
resultStatus = "Pass"
} else {
resultStatus = "Fail"
}
console.log(resultStatus) // output: "Pass"- In the above code block, we are using a constant
percentageand a variableresultStatus. The statementifchecks the conditionpercentage > 33. - If the condition
percentage > 33is true, the value ofresultStatuswill bePass. - Otherwise, the value of
resultStatuswill beFail. - Now, we will replace the
if...elsestatement with a ternary operator:
const percentage = 60
const resultStatus = percentage > 33 ? "Pass" : "Fail"
console.log(resultStatus) // output: "Pass"- That's how the ternary operator makes our code more cleaner and readable.
- In the above code block, we are using the string
PassandFail. But We can also use a function. For example:
function printFail() {
return "Fail"
}
function printPass() {
return "Pass"
}
const percentage = 30
const resultStatus = percentage > 33 ? printPass() : printFail()
console.log(resultStatus) // output: Fail- If condition
percentage > 33is true, functionprintPass()will be executed. - If condition
percentage > 33is false, functionprintFail()will be executed.
How to Use Nested Ternary Operators
const percentage = 60
let resultStatus
if (percentage > 33) {
if (percentage >= 60) {
resultStatus = "Pass with First Division"
} else {
resultStatus = "Pass with Second Division"
}
} else {
resultStatus = "Fail"
}
console.log(resultStatus) // "Pass with First Division"In the above code block, we have two conditions to check:
- If
percentage > 33is true, then we again check ifpercentage >= 60.- If
percentage >= 60is true, then we assign the valuePass with First Divisionto variableresultStatus. - Otherwise, we assign the value
Pass with Second Divisionto variableresultStatus.
- If
- If
percentage > 33is false, then we assign the valueFailto variableresultStatus.
const percentage = 60
let resultStatus =
percentage > 33
? (percentage >= 60 ? "Pass with First Division" : "Pass with Second Division")
: "Fail"
console.log(resultStatus) // output: "Pass with First Division"- In the above code block, We check condition
percentage > 33using the first?. - Then, we check condition
percentage >= 60using the second?that is nested under the first?. - The following figure shows the nested hierarchy for the above code block:
percentage > 33
|
---------------------------
| |
? :
percentage >= 60 Fail
|
-----------------
| |
? :
Pass with Pass with
First Division Second Division Note: Do not use nested ternary operator if not necessary. As they create more confusion and our code becomes hard to read.
Conclusion
- Ternary operator is a conditional operator in javascript.
- Ternary operator executes the expression based on truthy or falsy value.
- Syntax for the ternary operator is
condition ? firstExpression : secondExpression. If the condition is truthy,firstExpressionwill be executed. If condition is falsy,secondExpressionwill be executed. - We can use a ternary operator in place of
if..else. But the Ternory operator can't completely replaceif...else. - Ternary operator helps to keep code neat and clean. However, we should avoid nested Ternary operators.
FAQs
What is a simple example of a ternary operator?
Consider, you want to check the eligibility for a Vote. Then we can use the ternary operator as: age < 18 ? "Not eligible for Vote" : "eligible for Vote".
When should I use the ternary operator?
Instead of a conditional statement if...else, you can use a ternary operator. Ternary operator helps to make simple decisions based on truthy and falsy values.
