The Conditional (Ternary) Operator
❮
❯
Example
If the value of age is < 18, set the value of text to "Minor", otherwise to "Adult":
let text = (age < 18) ? "Adult" : "Minor";
Try it Yourself »
Description
The conditional operator is a shorthand for writing conditional
if...else
statements.
It is called a ternary operator because it takes three operands.
Syntax
(condition) ? expression1 : expression2
Parameters
Parameter | Description |
condition | Required. The condition to be tested. An expression that evaluates to true
or false . |
Browser Support
() ? x : y
is an ES1 feature (JavaScript 1997).
It is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
❮
❯