Ternary Operator

The ternary operator is a shorthand way to write an if-else statement. It works by evaluating a condition and returning one value if the condition is true, and another value if the condition is false.

The syntax of the ternary operator is:

Loading Code . . .

In the code above, the canVote variable is assigned the value of "yes" if age is greater than or equal to 18, and the value of "no" if age is less than 18.

As the age variable is 18, canVote is assigned the value of "yes".


In the exercise below, use the ternary operator to assign the value of 'odd' or 'even' to the evenOrOdd variable based on the value of the num variable.

The num variable will receive a random number.

Loading...
Loading...
Loading...
Loading...

© 2024 - ®Mewters