This operator includes three operands: a condition followed by a question mark (?) sign, and two expressions separated by the colon (:). The first expression is executed when the condition is true, and the second expression is executed when the condition is false.
In the above syntax, condition, expr1, and expr2 are the three operands used in the ternary operator. The value is assigned to the variable 'a' based on the provided condition. The condition is evaluated as a Boolean value; based on its result, the operator assigns the result to the variable. It assigns the first expression on the true value of the condition and assigns the second expression on the false value of the condition.
This is a simple example of checking whether the number is odd or even using the ternary operator. The result will display using the alert() dialog box.Test it Now
After the execution, the output will be -