Out of all the skills I’ve learned in Code For Life, which are a lot, I think that If statements/ Conditionals is probably my favorite thing to use and it was my favorite thing to learn about. I mean, the possibilities are endless with this statement! It’s so cool to me that you can put any value in the statement and it’s used in tons of ways in things you use every day. If statements are used when you put in passwords, in video games, on a multitude of websites, surveys, It’s everywhere. Now, if you know absolutely nothing about code, which is a vast majority of America, then I got you. An If statement, sometimes called a Conditional, is a statement you can use in Javascript. First off, Javascript is one of the most used coding languages out there, so you should learn it sometime. But anyways, a conditional is used to have something happen when a set of conditions are true (hence the name “conditionals”) I’ll show you an example

if (person is “Joandy”){ Say Hi!; }

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.

“Is that Joandy?”-----------------------if person = “Joandy “Oh I’m going to say Hi!”--------------Say Hi!

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!

If (person = “Joandy”) { console.log(“Hello”); }

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):

if ( password == “SharksRKool”){ console.log(“access granted”); }

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:

console.log(“Joandy is Awesome”);

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:

if(password == “SharksRKool”){ console.log(“access granted”); }

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:

let password = “SharksRKool”;

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:

let username = “JoandyTheCoder”;

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:

if( password == “SharksRKool”){ console.log(“access granted”); } else if (username == “JoandyTheCoder”){ console.log(“access granted again) }

Just a reminder, a real sign-in authenticator would look nothing like this, but just for readers to understand what conditionals do, I used these terms. Let’s get back to work So as you can see, the else if phrase uses the same properties as a regular “if” would. Think of it as an extension. You must be wondering, what would happen if you didn’t set the value of username to “JoandyTheCoder”.  Well, I’ll tell you. Nothing. Since the username isn’t set to “JoandyTheCoder”, The computer won’t receive the “OK” to log the message “access granted” since the conditions haven’t been met. But what if you want something to happen if the password isn’t JoandyTheCoder. Well Javascript has you covered. So let’s set the value of username to “wonder woman” and password to “superman”. In order for something to happen if username isn’t what is said in the conditional, we must use the phrase else. This phrase accounts for any other value that is set to username and password and allows us to have something happen if any other value is equal to password or username. I know that sounded confusing but you’ll understand once you see this:

if ( password == “SharksRKool”){ console.log(“access granted”); } else if (username == “JoandyTheCoder”) { console.log(“access granted again) } else{ console.log(“access denied”) }