The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement.
The ternary operator ? is a way of shortening an if-else clause, and is also called an immediate-if statement in other languages (IIf(condition,true-clause,false-clause) in VB, for example).
The ternary operator is a concise way to write simple conditional expressions in a single line. It can be particularly useful when assigning values or constructing expressions based on conditions.
Additionally, the example is poor, you should not use ternary operator to replace a statement (if ..else in this case), the operator returns a value, which should be used.
The ternary operator is a syntactic and readability convenience, not a performance shortcut. People are split on the merits of it for conditionals of varying complexity, but for short conditions, it can be useful to have a one-line expression. Moreover, since it's an expression, as Charlie Martin wrote, that means it can appear on the right-hand side of a statement in C. This is valuable for ...
The ternary operator can be used to combine two expressions but an empty statement is not an expression. A method invocation can be used as an expression if the method returns a value (read: is not declared void) and it could be used together with a dummy constant null to form a ternary operator but the result would be again an expression which is not allowed in places where a statement is ...
Please demonstrate how the ternary operator works with a regular if/else block. Example: Boolean isValueBig = value > 100 ? true : false; Exact Duplicate: How do I use the ternary operator?