Learn JavaScript Episode #6: Comparing Values

Alright guys, so we’ve learned about
boolean operators, strings, integers, etc. We learned how to redefine variables.
What we’re going to be learning about in this video is how to compare values. So,
the most basic way to compare two values to check if something is true, and
that’s what we’re doing essentially we’re checking if something is true or
false. It’s called a truth checking, I guess. What we’re going to be doing
first is checking what we’ve done is we’ve checked if something equals
something else, and again you can say does true==true, and that’s going to say
true. Or, we can say does “hi”==”hi” and it’s going to return true. But, if we say “hi”
==3 it returns false because it is not the same. So you use two equal signs to compare values, and you use one equal sign to assign values. So if I said “hi”=
3 it’s going to freak out because it doesn’t know what I’m doing there. A string never goes on the left of something, or an integer. You can do 5=6 and
invalid left hand side in assignment. So, basically it knows we’re assigning a
value to something because we’re using the equal sign we have a
value there for it, but we can’t assign that to an integer. We can assign it to a
variable, and so if we create a variable called mynum we can set it to 6. Now
every time we print out mynum it’s going to use 6. So here we could say is
mynum==6, and that’s the most common reason you’ll find
fact-checking, or truth checking, is to see if a variable is equal to something.
So, let’s say that I have started a website that can let people sign up, and
if they choose to they can purchase a premium service, or a subscription. So, I’m going to create a variable let’s assume that I’m a user right now and my
variable is set to isPremiumUser, and I’m not. So, let’s say we only want to
provide a bit of code or we want to do something special if the person is a
premium user. So, we would compare that isPremiumUser, and is that
equal to true and if it is we can do something, but it’s not so we’re not
going to do anything, and that brings us into if statements. If statements are
basically a way to apply this and do something based on the result,
whatever we want to, but we’re not quite done with comparisons. We can also
compare if something is not equal to something else. So, if isPremiumUser is
not equal to true then maybe we’ll say, hey, you should buy this, you
know, or you should subscribe as a premium user. So, the operators to use
when comparing if something is not equal to another one is to use an exclamation
point followed by an equal sign, and these operators right here means is not
equal to. Basically, in programming, not just in JavaScript but in PHP and other
languages, the exclamation sign means not. So by combining it here you’re saying not
equal, basically. You can also compare greater than or less than. So we can say
is 5 less than 7, and it is true. Now we can also say is 7 less than 7 and
it’s going to say false, but if I said is 7 greater than 7 it also says false. So, maybe we want to run a bit of code, and I’ll show you guys
examples as soon as we get into if statements, but maybe we want to compare if a number is greater than or equal to a number. So, we would say is 7
greater than equal sign 7. It’s going to return true because 7 is equal to
7, it’s not greater than. You can do the
same with a less than. 7 is less than or equal to 7, and that’s going to return
true as well. So, with all that said, let’s start writing if statements. And for if
statements we are going to move back to our IDE, and we’re going to be running
this page every time we want to update it.


Add a Comment

Your email address will not be published. Required fields are marked *