The content in there isn’t 100% real code of course, but just so you have an idea, I used those words. So as you can see in the parenthesis, it says ‘person = “Joandy”’. This means that if the value person is equal to the word Joandy (Which is my name) then say Hi!. If you are still confused, try putting it into a real-life scenario.
Once again, this is Not real code. But anyway, it is basically asking if the person is Joandy, and if it is, then say hi! But if you’re curious about what it might look like in real code, then say no more!
There probably wouldn’t be code this simple on a professional website, but it’s a great exercise!!! Like I said before, there are tons of things that you can do with these. One of the main places you’ll see conditionals would be when you put in a password for your favorite sites. Let’s say that I made an account for Instagram and my password was “SharksRKool”, and I log in. Now in this situation, the computer would compare your set password to the password that you put in. The code might look like a little bit like this (Only as an exercise):
So before we move on, we should discuss what a variable is. As you can see, the conditional is asking whether the password is equal to the words “SharksRKool”. So in this case, I would have to set a variable ( This variable is named “password”) to the password that you set previously. But before you get confused, a variable is a word that holds a value in code, so in this exercise, we set the word password to hold the value of “SharksRKool” so we can compare password to the text that was entered in the future. Now, the next step is to understand what the “console.log” is. console.log is a pretty simple command that just enters text into the console. For example:
From that, you put in the words “Joandy is Awesome” into the console. If you want to put in specific text you MUST use double quotes or single quotes to wrap around the text. You can also console.log a variable, and whatever value is assigned to the variable will be entered to the console, and you DO NOT have to use quotes for this. So now that you understand everything about what the code consists of, let’s break it down to what it does. So as a reminder, this is the code we use:
So in the parenthesis, we compare the password that we set before to the words “SharksRKool”. In order to see if this works, we have to set a variable of password to the words “SharksRKool” prior to this. Here’s an example of a variable:
So now the computer is going to ask whether or not the value of password is equal to “SharksRKool”, which is now true! So since this is true, it is going to log the message “access granted” to the console. Let’s take this a step further and try to code a validation for a username. So first you would start off by declaring a variable named username like this:
So now that we have declared what our username is, let’s try to build this into the conditional we have already made. In order to this, we need to use the phrase else if. This phrase allows you to add more conditions without having to write a whole new statement. So this is how it would look: