Let's talk for just a moment about the conditional ternary. operator. ternary means three or three parts. And so this is an operator that has three parts to it. It is usually used normally used as shorthand for an if statement. That's the reason we're talking about it in the conditional section.
It is a quicker way to enter in if statements. So if your if statement only is concerned with a single expression, a ternary operator may be a possibility, and it is less to enter. Let's take a look at an example. I'll open the console. Now you may remember as we first started talking about if statements we dealt with an if statement that determined the age that was entered and then indicated whether they could vote or not. So we had a variable age and we set it to 18.
Okay. Now this is how we would use the, the ternary operator, I'm going to set another variable message. I'm going to set this equal to and here comes the ternary operator, I'm going to put a condition, I'm going to put put a condition inside of parentheses. The condition we want to check is whether age is greater than or equal to 18. Now if that condition is true, it will do the expression that follows the question mark. So we want it to return you are able to vote otherwise if it is false, it will return what follows the colon Okay, So we have our expression.
I'm sorry, we have our condition. Then we have expression one, expression two. So really quick, let me write that out so we can understand it. This is basically the structure of the ternary. operator. We have a condition, we then have a question mark.
We then have expression one. We then have a colon. We then have expression two. So we have a condition. If that condition evaluates to true, it returns expression one if the condition evaluates to false, it returns expression to another way to state it. If the condition evaluates to true, it returns what's following the question mark.
If it evaluates to false, it returns what's following the colon. All right, let's go ahead and see this work. Let's console dot log message now. Right, enter this right it'd be much better and indicates you are able to vote. If we change age to 17 then go back to the if statement. And then go back to whoops.
Console dot log, you are not able to vote. So a shorthand way of entering an if statement. That's what the ternary operator is good for.