In the above example code a ternary operator has been used to test and print whether the variable number is less than or equal to 7.
With the help of the ternary operator we can reduce a multi-line if...else statement to a single line of code.
The use of the ternary (?) operator is very simple. First we write an expression to be evaluated followed by the ? symbol. Then we write the code to be executed if the expression evaluates to true followed by the : character and then the code to be executed if the expression evaluates to false.
Example code follows:
var number = 10;
number <= 7 ?
" number is less than or equal to 7 " :
" number is greater than 7 ");
The above code has been broken up into several lines for readibility and the same could also be written on a single line.