Well welcome back.
So the next few videos I'll cover a really important topic which is how do we make decisions in our
Javascript code.
And the first step is learning about something called boolean logic.
So if you can recall back to the first few videos we talked about a data type in javascript called billions
and billions are very simple.
There's only two possible options for a boolean value.
True or false.
So boolean logic is simply writing statements that evaluate to be true or false.
And eventually we'll use these statements to actually run certain parts of our code if something is
true and some other parts of our code if something else is false.
So these statements can be things like.
A user is logged in.
That's true or false but it could also be something as simple as if subtotal is greater than $10000
.
And those of value to be true or false.
And we do something with them in our code.
So the first thing we have to start with are comparison operators and comparison operators are things
that most of us use all the time in our day to day life.
It's how we compare two values to one another and it's how javascript does the same thing.
So this chart here goes over all the comparison operators.
It assumes that x is equal to 5 and you can plug in five wherever you see X..
So let's start with the simplest one greater then.
So x is greater than 10 is five greater than 10.
No.
So that evaluates to be false.
Next is greater than or equal to five greater than or equal to five.
That is true.
Next we have less than and less than or equal to.
You can see that Plug-In 5 5 is not less than negative 50 so it evaluates to be false.
Five is less than or equal to 100.
So this evaluates to be true.
The next four are a little bit different.
They cover equality and non equality.
So it's how we check if X is exactly equal to the number 5 or x is equal to that string 5 or X is anything
but the number 20.
It's how we basically check for equality.
One thing you'll notice though is that there are two ways to check for equality.
So there's one with two equal signs and one with three equal signs.
Likewise there are two ways to check for quality.
One with one equal sign and an exclamation point and another one with an exclamation point and two equal
signs.
So they both kind of go together in pairs.
We'll start by talking about double equals here.
Notice that X double equals the string five.
So X is number five x double equals the string five is true.
But if we do X triple the cross the string five that's false.
And the reason for that is on my next slide here it has to do with something called type coercion.
So when we use double equals it performs type coercion.
And what that means is it basically takes the two numbers or the two strings or the two variables whatever
they are.
And it tries to turn them into a similar type so that I can compare them.
So in this example x is in number 99.
And when we double equals x with the string ninety nine that is true because javascript performs type
coercion.
It tries to get them to be the same format and then compare the value versus triple equals does not
perform type scores and it cares not only about value.
So not only that they both say 99 but that one is a string and one is a number which is not valid triple
equals.
So as a rule of thumb you should always use triple equals it's much safer.
It's much more specific and it's not that as another example you can see here if we have no bar Y is
equal to no no double equals undefined it's actually true even though they are very different values
.
They're not the same thing.
Double equals considers that to be true and triple equals considers that to be false.
So the last thing that I want to do here is show a few interesting cases of using double equals.
And it basically behaves in ways you definitely would not expect.
So this is just another warning of why you should not use it.
But it also reveals a few of the underlying quirks in Javascript.
So we can take the number excuse me the boolean true and double equals the string 1.
And that's actually true.
However if we do that with any other string number.
So if I say true.
So just to show you two equals 1.
That is true but true equals 12 is false.
Likewise I say zero doubles equals false.
And that is true.
No double equals undefined as we just saw is true.
And probably the weirdest one is not a number.
Double equals not a number is actually false even though they look like exactly the same thing.
It's one of the weirdest quirks in javascript.
The way that not numbers implemented it is actually not comparable to not a number.
So the next thing that we're going to talk about is how do we actually change some of these together
and make those more complex statements that we talked about earlier.