Learn Node.js unit testing with Jest, using mocking, snapshots, and best practices to ensure reliable and efficient application performance.
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.
true or false.conditionalExpression ? firstExpression : secondExpressionconditionalExpression is true, the ternary operator will execute firstExpression.conditionalExpression is false, the ternary operator will execute secondExpression.const finalValue = conditionalExpression ? firstExpression : secondExpressionif Statementif statement with a ternary operator.const percentage = 60
let resultStatus
if (percentage > 33) {
resultStatus = "Pass"
} else {
resultStatus = "Fail"
}
console.log(resultStatus) // output: "Pass"percentage and a variable resultStatus. The statement if checks the condition percentage > 33.percentage > 33 is true, the value of resultStatus will be Pass.resultStatus will be Fail.if...else statement with a ternary operator:const percentage = 60
const resultStatus = percentage > 33 ? "Pass" : "Fail"
console.log(resultStatus) // output: "Pass"Pass and Fail. 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: Failpercentage > 33 is true, function printPass() will be executed.percentage > 33 is false, function printFail() will be executed.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:
percentage > 33 is true, then we again check if percentage >= 60.
percentage >= 60 is true, then we assign the value Pass with First Division to variable resultStatus.Pass with Second Division to variable resultStatus.percentage > 33 is false, then we assign the value Fail to variable resultStatus.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"percentage > 33 using the first ?.percentage >= 60 using the second ? that is nested under the first ?. 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.
condition ? firstExpression : secondExpression. If the condition is truthy, firstExpression will be executed. If condition is falsy, secondExpression will be executed.if..else. But the Ternory operator can't completely replace if...else.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".
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.
