

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Conditionals
incomplete
2: Comparison Operators
incomplete
3: Logical Operators
incomplete
4: Switch
incomplete
5: Ternary Operator
incomplete
6: When to Ternary
incomplete
7: Truthy and Falsy
incomplete
8: Nullish Coalescing
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Sometimes using 3-5 lines of code to write an if/else block is overkill. The ternary operator makes it easy to write a conditional as a single expression.
const price = isMember ? "$2.00" : "$10.00";
I like to read it in English as:
If
isMemberis true, evaluate to$2.00, otherwise evaluate to$10.00.
The same logic using if/else would be:
let price;
if (isMember) {
price = "$2.00";
} else {
price = "$10.00";
}
Ternary's latin root means "3", and it's the only JavaScript operator that takes three operands.
:)On line 6, create a variable called messageStatus and use a ternary operator to set the value of that variable to either:
"Processing": the number of retries is less than the limit"Failed": the number of retries is greater than or equal to the limitUse the ternary operator and a comparison operator to dynamically determine which string to set as the value of messageStatus to.