We’re first going to look in analyze at what attorney operator would look like in a React application. Then we’re going to go through two examples. We’re going to start off with a basic, just very similar to an if/else conditional. And then we’re going to see how you can implement compound conditionals directly into the ternary operator. As always, I’ll be following along in the comments section. So if you have any questions, comments, recommendations, anything like that, feel free to add those and I’ll get back to them as soon as I can. And if this video was helpful along your own coding journey, please give it a like and subscribe so I can keep on making more of them. So with all that being said, let’s dive into the code.
This is where the ternary operator comes in. I have to write this all on one line. And what a ternary operator allows you to do is to do that. Is to write an entire conditional on a single line. Here what I could say is, has permission and then I’m going to do a question mark, and then we’ll say active and I’m making all of this up right here. This is just an example to show what you may build. Then we’re going to get into real examples later on. So I could say active colon and then disabled. So what I have done here is I’ve provided a conditional. So this is the same thing as saying if, has permission then I want you to return active and if not, I want you to return disabled. This is the only way or the proper way I should say, for building a conditional in tools like React or Vue so that you can have some dynamic behavior built directly into your HTML and your JSX. So this is the main reason why ternary operators are so important to learn because if you are building out any kinds of real world front end application, you’re most likely going to have to build in something like this at some point or another.
Here I could say, if the age is greater than 25, then I want to console log, can rent a car and then right here we want to provide an else statement and then say I want that to be console log is not old enough yet. And that’s all that we want this function to do. So it’s pretty basic then I’m going to call this function by saying age, verification, if I say 15 here, if I save and then run it, it’s going to print out that the user is not old enough. If I change this to 55, and then run it again, now it’s going to say they can rent the car. So this is all working properly. It’s an incredibly basic function and this conditional is pretty much as basic as you can get. I did that on purpose. Anytime that I’m wanting to learn something new, especially something that might be a little bit more on the confusing side, I like to start off with a base case. We’re going to simply comment this out and then below here, I’m going to show you the syntax that will allow you to have a ternary operator.
We’re going to use the exact same logic, we’re just going to switch it up and use it with the ternary syntax. Now the way that you can do this is I’m going to store it in a variable. I’m going to say, let, answer, equals. Then here, I’m going to say age greater than 25, and then a question mark. Then I’m going to have it say the same thing. So I’ll say I can rent a car and then colon can’t rent a car. You could put is not old enough, whatever you want on that side. So that’s going to store it in a variable and then let’s simply print out the value of whatever that variable is. So we’ll say console log, answer and don’t worry, I know I typed all of that out pretty quickly we’re going to walk through exactly what the mapping is doing and everything.
Let me hit clear, save and then run it. And now you’re going to see it says, “They can rent the car.” So we’re getting the exact same answers before if I change this to five years old and hit run, it’s going to say they can’t rent the car. So this is working perfectly. This is the exact same behavior we were getting when we had that conditional on the five lines of code. So let’s walk through what’s going on. Right here, you can see the very first part of a ternary operator, is going to be the conditional. This is exactly the same as saying, if age is greater than 25 and so this is the first part you’re going to want to break your ternary operators, it’s easiest way to think of them is that they’re broken into three parts. The first part is the conditional. After the question mark, the second part is going to be, if that conditional is true, I want you to run this code.
Now if it’s not, you have the colon, and now this is going to be what happens if the condition was not met. If this is false, if the age is not greater than 25, then it’s going to skip everything here and then it’s going to parse the colon and say, “Okay, we want to return whatever is in this value here.” Now I also could have written out the console log statement here if you’re curious on why store this in a variable it’s really just for the sake of space because the ternary operators can get a little bit long. So I could have done something like this.
I could have just said age is greater than 25, then I could have said console log, can’t rent a car. Then here, console log again and then can’t rent a car, just like this. Now if I save this, clear it and run it, then you’ll see that we get the exact same behavior where it says, can’t rent a car. But typically, because of the way that this works, whenever you have … And let me get rid of all of that, just so it’s out of the way. So whenever you have a situation where you’re using a ternary operator, typically you do not want to put your console log statements actually in the true or the false values here. So let’s walk through the mapping just to make this clear, because I cannot tell you how … Don’t worry if this looks weird. I can’t tell you how many times a student has come up to me and says, I do not like using ternary operators, they don’t make any sense they look weird. I can tell you it just takes practice and what helped me the most when I was learning them is understanding what the mapping was.
Remember, the very first part of it is the first part of the conditional. We have, if age is greater than 25, we have age is greater than 25. They don’t have the if here, but you can just imagine that the F is right in front of it. then the question mark means that we’re now going to break into whatever happens when this is true and when it’s false. This right here is the true part. This is like dropping here into line three where it says, “Can’t rent a car. Then after this, this little colon here, you can imagine that this is the else. So this is exactly what we have here on line four where it says else and then it says, can’t rent a car. It’s exactly what we have here where it says is not old enough.
This is the basic way of implementing a turnaround operator. Like, you can see if you ever wanted to implement this in a React application or a Vue app, you couldn’t write the code like this. If you wanted to put it on one line directly into the HTML, then you’re going to have to write it like we have right here. So that’s why it’s important to know. I’m going to get rid of all of this and now let’s get into a little bit more of an advanced example. Before we get into this, I want to add the caveat that what I’m about to show you is important to understand that I would not recommend using it on a regular basis because the single ternary after you practiced it enough it will actually start to become very familiar to you and it’s not going to look as weird as it may look the very first time or second time that you’ve seen it.
What I’m going to show you now is, how you can implement compound logic. So multiple conditions all into the same ternary operator, and I can tell you this is going to look very weird. I’ve been doing this for a number of years. And when I see a compound conditional built into a ternary, it still takes me a while to kind of dissect the code to see exactly what’s happening. So I wouldn’t recommend doing this. But it is important to understand because I have run into a number of projects that I took over and I worked on, where the developer did do this. And it was important for me to understand what their logic and what their process was, because if I didn’t, then I’d be lost in the code base. So let’s walk through a more advanced example. I’m going to create another function here, I’m going to call it admin controls. So we’re going to say that the purpose of this function is to either show or hide admin controls.
Here I’ll say if user.admin and same thing we’re expecting a true or false value. And if the user is an admin or not. Then inside of here, I’m just going to console log and say, showing admin controls. Just like that. Then if they’re not an admin, we need to have some logic for that. So I’m going to say, if not, then I want to console log and say you need to be an admin. Then we also need to verify, we need to have a backup for if the user is just a guest user. Maybe the user hasn’t signed in. Here I’m going to put another conditional and say else and then we’ll put console log and then you need to be logged in, just like that.
Now if I save and I run this, we should have the, you need to be an admin. So we still have our showing admin controls and now it says, “You need to be an admin.” Everything there is working perfectly, they dropped into this because it was true. But then they were not an admin, so it fell into the else statement. Everything there is working. And last one, let’s say user three, and this is our guest user. So this is going to be a user who doesn’t have any values whatsoever. Just guess a user three and we’re just going to say that there, no. Now this user three they should return that you need to be logged in. Let me clear this, hit run and it should say, “You need to be logged in.” Which is perfect.
Everything here is working and our conditional is working. But let’s imagine that you need to put this in a ternary operator. Now this is going to look very weird. Once again, I would not recommend doing this at all, just because I think it leads to unreadable code. But if you ever come across a ternary operator that looks like this, you’re going to know exactly what it’s doing. I’m going to create a variable that we’re going to store this. So I’m going to say let, response equal and then here we are looking … Let me comment all this out, just so you don’t get a false reads on it. We have this user argument. I’m going to say user, then from there, we’re going to start off the same way we did with our basic examples.
It’s going to say, user, question marks where we’re going to first check, is the user true. Then this is where it gets weird. This is where we’re going to place another ternary operator inside of the very first statement. Because the way that this logic works is it’s going to check to see, is this the case? Is this user, do they exist? Same thing as saying user true and then we’re going to drop in to what happens if it’s true? Well what happens when it’s true in this example? Well we drop in to this second conditional. That’s exactly what we do here with the ternary operator. We’re going to say, user.admin and then we’re going to give another question mark. Because this is like asking that second question. We’re going to say, user admin and then here showing admin controls … and then we’re going to give what happens if they’re not an admin. You need to be an admin.
Now that we have that, now we need to go in to that final else. This is what happens if the user didn’t exist. Now we’ll say, you need to be logged in. Okay? We stored all of that in the response. Let’s just console log that, we say console log response. Hit Save, clear this and we should get all of the exact same answers. If I hit Run, there you go. You need to be logged in right above it was you need to be an admin and then showing admin controls. As you can see, this looks really weird. One thing I will say is unlike the if/else conditional, the spaces and having these carriage returns where you have all of the code on different lines, that may not be allowed in the JSX, or in your React or Vue application. But you actually can have carriage returns and it is valid code.
Let’s walk through what’s going on here, kind of line by line. Let’s start at the top. We’re checking to see if the user exists. That’s a same thing as just placing the user right there. Same thing as saying, user === true. If that is true, they will drop down into this next line here. If you prefer, if this is messing you up too much, then let me put this all on a single line again, just so we can read it all from left to right. Right here, what we’re doing is we’re saying is the user, do they exist? Yes. Okay. Well now it’s time to drop into a nother conditional.
One thing that does help me whenever I’m working with this kind of code, is I like to wrap the separate ones up in parens, just like this. This makes it a little bit easier and as you’ll see, this also works exactly the same way. Now if I hit Run, everything still works but I think at least in my opinion, this is a little bit easier to read and it shows that this is a nested conditional. Technically, you could keep on nesting them. You could have another conditional here, or you could have it in the else block. But to me, even having two of them is honestly a little bit too much. But it’s your world, you get to live in it. So build your conditionals however you want. But I just hope I wouldn’t have to take over your code base if that is what you decide on doing.
What we’re just getting back to, we have the user first conditional. They drop in to this conditional. This is the same thing as what we have here on lines three through seven. Where it says, “If this is the case, I want you to show the admin controls. If not, I want you to … You need to be an admin.” Same process. Then finally, if the user didn’t exist. If this was false, then it skips everything here until it finds the final colon and then it says you need to be logged in. What we wrote here on line 12 is exactly the same as what we wrote on line two to 10. As you could see the behavior is identical but the difference is, if you ever need to write your conditional all on one line, then this is the syntax that will allow you to do that.