Concept Flow - Ternary operator
Evaluate condition
Yes/No
True
Return true_value
End
The ternary operator checks a condition and returns one value if true, another if false.
const age = 18; const canVote = age >= 18 ? 'Yes' : 'No'; console.log(canVote);
| Step | Condition (age >= 18) | Result | Value assigned to canVote | Output |
|---|---|---|---|---|
| 1 | 18 >= 18 | true | 'Yes' | No output yet |
| 2 | N/A | N/A | N/A | Print 'Yes' |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| age | undefined | 18 | 18 |
| canVote | undefined | 'Yes' | 'Yes' |
Syntax: condition ? true_value : false_value Evaluates condition first. Returns true_value if condition is true. Returns false_value if condition is false. Used for quick if-else assignments.