JavaScript ? : Conditional (ternary) Operator
The conditional (ternary) operator picks one of two values based on a condition: condition ? valueIfTrue : valueIfFalse.
Description
The conditional operator, also called the ternary operator because it takes three operands, evaluates a condition and returns the second operand if it's truthy or the third if it's falsy. It's a compact alternative to if...else when you need a value, such as inside a template literal or JSX.
Ternaries can be chained to handle several cases, but deeply nested ones are hard to read; an if statement or a lookup object is often clearer.
It's the only JavaScript operator with three operands. Only the chosen branch is evaluated, so the other expression's side effects don't happen. Because it has low precedence, wrap it in parentheses when you use it inside a larger expression, such as string concatenation: 'You are ' + (age >= 18 ? 'an adult' : 'a minor').
Properties
- Operator
? :- Category
- Other
- Precedence
- 2 (1 is lowest, 18 is highest)
- Associativity
- right-to-left
Syntax
condition ? exprIfTrue : exprIfFalseExample
Browser Support
The following information will show you the current browser support for the ? : operator. Hover over a browser icon to see the version that first introduced support for this operator.
Desktop





Tablets & Mobile





