Conditional (Ternary) Operator Summary Evaluates expressions conditionally. Syntax condition ? expressionA : expressionB Parameters condition The condition to evaluate and check if true. The condition can be any valid expression. expressionA The expression to execute if the condition evaluates to true. expressionB The expression to execute if the condition evaluates to false. Description The conditional operator is used for evaluating expressions conditionally. It can be used inside expressions in place of an if...else statement since if...else statements cannot be used where expressions are expected. The expression begins by evaluating the condition, the first argument to the operator. If the condition evaluates to true, the evaluated value of the expression between the ? and : (the second argument to the operator) is returned. Otherwise, if the condition evaluates to false, the evaluated value of the expression after the : (the third and final argument to the operator) is returned. The conditional operator is the only JS++ operator that is ternary; in other words, the conditional operator is the only operator in JS++ that takes three arguments. Examples Using the Conditional Operator instead of an if-else Statement 12345678910111213import System; int x = 1; Console.log(x == 1 ? "x equals 1" : "x does not equal 1"); // "x equals 1" // The following if-else statement has the same effect:if (x == 1) { Console.log("x equals 1");}else { Console.log("x does not equal 1");} See Also if...else statement Share HTML | BBCode | Direct Link