JavaScript Tutorial ✦
Ternary Operator
Shorthand for if-else statements
This is a quick way to check a condition and choose between two values.
Format:
condition ? valueIfTrue : valueIfFalse
Example:
age >= 18 ? "You are an adult" : "You are a minor"
Explanation:
- First, write the condition you want to check (e.g.,
age >= 18
) - Then, after the
?
, write what should happen if the condition is true - After the
:
, write what should happen if the condition is false
const result = age >= 18 ? 'Adult' : 'Minor';