Transcript of the tutorial.
TOP
So in the last video we saw that we could compare different things.
And one of those comparisons was this equality right here.
So we use double equals to see if two things were the same.
So we have a variable age.
We've set it to 25 and we're checking if that equals 25.
Now, obviously, this is going to return true?
It does.
Now this double equals sign right here.
This is known as loose comparison, meaning that different types can still be equal.
For example, let me duplicate this and I'm going to see if age, which is the number 25, is now equal
to the string 25.
So what do you think will happen?
Let me save that and preview and we get true.
So JavaScript is saying these two things are equal, even though they're different types.
And what's happening is behind the scenes, JavaScript is looking at this comparison and it's converting
this string into a number, the same type as this before it evaluates it.
Okay.
So when we compare two things to each other using a double equal signs, then different types can still
be equal because of this type conversion that JavaScript does in the background.
So let me just do another couple of examples.
I'm going to comment out the first two here and then up here I'm going to replace these with not equals
and not equals and I'm going to make this a number 25 and this is string 25.
So we're saying here age is not equal to 25.
Now, that would be false because age is 25.
And we're saying here age is not equal to the string 25.
But remember, the string is converted into a number, first of all.
So behind the scenes, it's still the same thing as this.
So this should be false as well.
So let me save that and preview and we get two falses.
Okay.
So that's fine.
We can do this loose comparison using two equal signs, but it's not always the most accurate way of
checking something.
So most of the time I think it's better to use what's known as strict equality.
And we do this by instead of using two equal signs here, we use three, and instead of one here we
use two.
So let's do a few examples.
I'm going to come down here and I'm going to say console dot log age is triple equals to 25.
Now this right here, this triple equals, this is strict comparison.
So we're saying is age the same value and type to this?
Okay.
25 Well, yeah, in this case it is.
So we should get true, which we do.
But if I do another example now and change this to a string 25 then now this should be false because
we're checking the type as well.
And 25 the number is not equal to 25 the string.
There's no type conversion going on behind the scenes.
JavaScript isn't taking this string and converting it into a number before comparing them.
Instead, we're comparing the number with the string directly and they are not equal.
So if we save this, then we see false because really a number can't be equal to a string.
Okay, two more examples.
This time I'm going to change this to exclamation mark.
Double equals and exclamation mark double equals.
We'll change this to a number right here as well.
25 So whereas before we had this and then one equals, now we have that and then two.
So this is a strict comparison.
Again, it's taking into consideration now the type and not doing any type conversion behind the scenes.
So is age not equal to 25?
Well, that should be false because age is equal to 25.
But is age not equal to the string 25?
Well, that's true because we're using strict comparison now, and this is not equal to this.
Okay.
So let's save it and preview and we can see false and then true.
So false and true just what we expected.
So most of the time it's better to use strict comparison because it results in more predictable results.
And probably 99% of the time, that's what I'm going to be using going forward.