All right, let's do one more. If conditional, we're going to declare a variable age set equal to 18. Since I'm recording this during an election year, let's determine if the person can vote. So if age, now we're going to use a new operator. I have not yet introduced conditional operators, but now we'll use one and then we'll look at them in more detail in the next movie. So we want to see if age is equal to or greater than 18.
Okay. 18 being the voting age. So let's go ahead and determine that so we put a greater than symbol, then we put an equal sign and that means greater than or equal to and Put 18 there and we put our curly braces is great if it is greater than or equal to 18. Then we're going to console log. You are old enough to vote and then put a semicolon at the end. I want to do one more thing I want to declare one more variable so I'm going to put a comma come across and enter variable can vote.
And that's all so if they are old enough to vote, I'm going to set the can vote variable equal to true outs. Console dot log. You are not old enough to vote. And now let's set the can vote variable equal to false. All right, so let's go ahead and save this. And then run this in a browser.
I'm gonna refresh. This page says you are old enough to vote. Let's change that to 17. Refresh again, you're not old enough to vote. Great. Okay, so I'm going to make one more change the way we're doing this.
Notice that in this case, we checked to see if the voting age if the age variable was greater than or equal to 18. If so, they're old enough to vote and then we set the can vote variable to true. Well, how about if we do this instead of logging? Let's set can vote equal to false. I'm just showing you another way to do this sort of problem, because it will get you thinking more about how you use if statements how and how you can Use Booleans. And so I initially set the can vote variable to false indicating that the person cannot vote, then I do a single if statement without an else.
No else clause and I say if the age is greater than 18, then we change and set can vote to true. Then down here we do simply one more if statement if can vote that's all we have in and notice this does not have an operator. There is no operator in there. It is simply checking to see if this evaluates to true or false. And notice we're setting the variable to true or false so it will evaluate to either true or false. If it evaluates to true, I'm just gonna put you can vote now.
So getting lengthy, else you can't vote console log Whoops, quotes, you can't vote. Okay. So now just another approach to this. Let's see how this one works. I'm going to save this. I'm going to jump out to a single page, refresh it, you can vote because we have it set to 17.
Right? So if I change that to 19, jump back or refresh the page, you can vote. Okay, so just another approach. Another way to show you if the conditional in the if statement, in this case is simply a variable, it's checking to see if it is true or false. We'll talk a little bit more about truthy and falsy in a subsequent movie. Alright, so so far in this movie, you have seen some basic, if statements, you we've checked to see if a username was available, by checking to see if if it were equal.
To undefined, we checked to see if a number was odd or even by dividing it by two, retaining the remainder using the modulus operator, and then checking to see if the modulo of the number was zero or some other number that determine whether it was true or false. And then finally, we played a bit with the age variable, checking to see if someone could vote or not. And we did that a couple of different ways. checking to see if it were greater than or equal to a number. Then we also use just a variable seeing if that variable was true or false. Okay, let's take a more in depth look at conditional operators.