Concept Flow - Ternary conditional operator
Evaluate condition
Yes
Use value if true
End
No
Use value if false
End
The ternary operator checks a condition and chooses one of two values based on true or false.
let age = 20 let canVote = age >= 18 ? "Yes" : "No" print(canVote)
| Step | Condition (age >= 18) | Result | Value chosen | Output |
|---|---|---|---|---|
| 1 | 20 >= 18 | true | "Yes" | canVote = "Yes" |
| 2 | End | - | - | Prints: Yes |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| age | 20 | 20 | 20 |
| canVote | undefined | "Yes" | "Yes" |
Ternary conditional operator syntax: condition ? valueIfTrue : valueIfFalse It evaluates the condition once. If true, returns valueIfTrue. If false, returns valueIfFalse. Used for simple if-else in one line.