Loading lesson path
< 18, set the value of text to "Minor", otherwise to "Adult"
let text = (age < 18) ? "Minor" : "Adult";let isMember = true;
let discount = isMember ? 0.2 : 0;let isMember = false;
let discount = isMember ? 0.2 : 0;The conditional operator is a shorthand for writing conditional if...else statements.It is called a ternary operator because it takes three operands.
( condition ) ? expression1
expression2
Required. The condition to be tested. An expression that evaluates to true or false. ? Required. The operator separating the condition from the expressions. expression1 Required.
The value to return if the condition is true.Required. The operator separating the expressions. expression2 Required.
The value to return if the condition is false.
The conditional (ternary) operator is the only JavaScript operator that takes three operands.() ? x : y is an ES1 feature (JavaScript 1997). It is fully supported in all browsers:
IE