Learn JavaScript #7 | If/Else Statements

JosephDelgadillo
58 followers
12 Video Views·Aug 17, 2024
Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/
In the 7th episode of this JavaScript tutorial series, we will cover if/else statements. I will demonstrate the basic syntax, a few practical examples, and why you should avoid "Yoda code."
Learn more about more courses at https://josephdelgadillo.com
Alright, so, in this video what we're going to be doing is learning about ways
that we can then use the variables that we create to do certain things. So, I'm
going to create a variable here called isPremiumUser. So, we start with the var
keyword and then we call it is premium user, and this is going to be false. So,
here what we're going to do is we're going to create an if statement that
basically determines you know if this is true then do this, if it is false then do
that. So, what you do to create an if statement you write the word if, and then
brackets, and you open and close them. And here is where you put your comparison
statement. So, just like in the previous video we're going to say if(isPremiumUser
== true) we're going to open up curly brackets and space it like that. So,
the syntax of this is the if keyword, opening bracket, and then your comparison,
and then your closing bracket, and then you open curly bracket, and you can close
the curly bracket, and all the code that's going to run if that statement
equals true is going to be written in here. So, for this instance what we're
going to do is just console.log("Thanks for being a loyal customer!"). I'm going to
save it, and when I refresh this nothing's printed out because I'm not a
premium user. If I change premium user to true and save it and refresh, then it
says Thanks for being a loyal customer! Again, if I change it to false, it's not
going to do that. Now, if you're always checking just what is true you don't
even have to put this part. It's going to do a basic comparison right
off the bat to check if the variable is you know if it exists and has a value, and if
it doesn't, and we can basically shorthand this. If we're checking if
something is true, we can erase all that and just type if and then the variable
that would either hold true or false. In this case it holds false so it's not
going to do anything, but if I write true here, save it, it's going to do this. Now,
this will work if a variable exists. So if I create a string that says
false inside of it, and refresh, it still prints that out because it's basically
saying if is premium user and it exists, it has string, so it equals to true.
We're going to leave this false actually and down here after we do the if
statement you can do an else statement, and directly after the closing curly
bracket here just write the keyword else, and then open up a new set of curly
brackets. And now this is going to be run if is premium user is not true. So I'm
going to write this out the long way just to simplify things here. So, if
(isPremiumUser == true) that's going to do this bit of code, else it's
going to run this. So, console.log("You should subscribe to our premium service.")
Save it and refresh, where it is false we see you should subscribe to our premium
service because we've set these premium user to false. So, what's happening is
saying if premium user then do whatever is in here, so the compiler or the
interpreter, doesn't even parse this code here if it is not true. It says
if this is true then do this, but if it's not then run this. So, for instance, we
could put a variable in here that doesn't exist, and it wouldn't freak out
or anything because it's not even parsing this code. But, if we set this to
true it's gonna freak out because it's going to say it's not
defined, we don't we haven't defined this. So, that was just to show you guys
basically that it only reads I guess what it needs to. I guess that's the
easiest way to say it. Now, we're going to create another variable and we're going
to learn about else if, so if else if else. It's going to make a lot more sense
once I write this out, so just bear with me. var myAge is going to be equal to
28; So, what we can do is we're going to create an if statement, and the
comparison here is going to be equal to, or less than rather. So, if(myAge) is less
than 1 we're going to open up our curly brackets, and we're going to
console.log("You are a baby.") Then what we're going to do here instead of just
writing else and then opening up curly braces which is kind of like a catch-all,
we're going to write else space if, and we follow the same format. So, myAge is
less than 3, open up your curly brackets, and we're going to print out console.log
("You are a toddler.")
And we're going to do this a few more times, so get into practice of it. I'm
going to walk you through a few more times just to make sure that we covered it,
and very well. So, after the closing curly bracket of the previous step, you're
going to write else if, you're going to open up your brackets, myAge is less
than 10. We're going to say "You're a big kid." else if(myAge) is less than 19,
actually we're going to say less than or equal to in this instance because 19 is
a teenager, and we're going to print out "You are a teenager." And, we're going to do
two more, so else if(myAge) is less than
40, we're going to say "You are an adult." else console.log("You must be old, you're
not in our records."). Save it, and then I'm going to run this. It's going to print
out that I am an adult. So what happens here, it might look that my age is
greater...so it's not less than 40 and it's not you know else, what I'm going to do
is change my age to 4. I'm going to save it, refresh, and it says you're a big kid even though my
age is less than 10 which is the condition it's capturing here. It's also
less than 19, but it's not printing this out, and my age is also less than 40 and
it's not printing this out, and this is because it stops at the first one where
the statement returns true. So, in this instance it says is my age less than
one? No, okay, move on. Is my age less than three? No, move on. Is my age less than ten?
Yes! So, what's between these curly brackets?
And then it reads that and it prints it out, and then it stops. It doesn't parse
any of this after that. So, you're going to have to write these in a
certain way so that they make sense. There are instances where if you write
it the wrong way the wrong thing is going to happen, and I'm going to try and
think of an example before I start the next video so that I can show you what
not to do, so that you're always going to be writing these correctly. So that's an if
statement, and how to do a very basic one to check if something is true or false
which we're comparing an equal sign. So, then we also compare an integer to
hard coded integers. This is something that you don't want to do. You don't want
to ever do it backwards. If one is greater than my age print out you're a
baby. Meaning that this would return true if my age is anything less than one, but
this is written backwards. We call this Yoda code because in Star Wars
Yoda talks backwards. So, you never want to write it like this, you always want to...
you're going to compare something to something. So the thing that we're
comparing is my age that always goes on the left. So, yeah, that's if statements
and else if and else, we're going to be learning more about these types of loops,
or conditional statements we call them, or rather just bigger chunks of code,
that's a better way to put it. We're going to be covering that more in the
next video.
