It consists of a condition followed by a question mark (?), then an expression to execute if the condition is true followed by a colon (:). Finally, the expression to execute if the condition is false comes thereafter. This operator is frequently used as a shortcut for the if statement.
condition ? value if true : value if false
Let’s take a look at a simple example:
The ternary operator is right-associative, which means it can be “chained”. This is similar to an if … else chain as compared below:
The above is equivalent to this: