? : 三元运算符 Ternary operator
Last updated: 2026-09-09
? : 三元运算符 Ternary operator
The ternary is the expression version of if...else: condition ? valueIfTrue : valueIfFalse.
| Category | Operators |
|---|---|
| ES version | ES1 |
📝 Syntax
const result = condition ? a : b;
Returns:One of the two values.
Mutates the original:No (returns a new value)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
adult
B
💡 Tip Ternary is an expression — usable in assignments and template literals.
⚠️ Warning Switch to if...else when chaining more than 2 levels.
🌐 Browser support
| Browser | |||||
|---|---|---|---|---|---|
| ? : 三元运算符 | ✓ v1 | ✓ v12 | ✓ v1 | ✓ v1 | ✓ v1 |
| Supported by all modern browsers | |||||
🏷️ Related Operators
💡 Related Cases
More case studies coming soon — check back later.
📚 Related Tutorials
❓ FAQ
QTernary or if...else?
ATernary for simple assignment; if...else for complex logic.
QCan ternaries nest?
AYes but keep it to 2 levels max.
QTernary precedence?
ALower than most — usually no extra parens needed.