02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Explicit Equality

Sometimes knowing if something is equal just isn't enough and we need to know if it is explicitly equal. This is a fantastic continuation from our last video that explained about matching between different data types. In this video we'll look into how explicit equality operators work - '===' and '!==='.

In the above comparisons, the values were said to be equal even if they are close to each other. But there are situations where we need to check for explicit equality. We want the output to be false even if there is minor difference between the two. The two values must be absolutely equal to satisfy the explicit equality condition. This operator uses the === symbol. Let's recap now: one equal sign (=) is an assignment operator, two equal signs (==) is an equality operator and three equal signs (===) is the Explicit Equality Operator.

Equality Operator (==) and Explicit Equality Operator (===)

Whenever we use the symbol ===, we mean that the comparison should be explicit. For example let us consider a Boolean variable isTrue whose value is true and trace the comparison using trace(isTrue==1). We know that the best alternative for true is 1. So the application gives the output as true because the equality operator (==) is used for the comparison. But when we use the explicit equality operator (===), the result is totally different. The compiler now wants both the values inside the trace to be absolutely equal. The fact that 'true is equal to 1' is not enough to satisfy the Explicit Equality condition. The result in this case would be false. But if we have trace(isTrue===true) or trace(1===1), the result would be true for both the cases as the values are explicitly equal.

Inequality Operator (!=) and Explicit Inequality Operator (!==)

The Explicit Inequality Operator works the same as the Inequality Operator but it wants the values to be explicitly unequal, the reverse of the above: we check for inequality rather than equality. Let's see an example to differentiate between (!=) and (!==). If we write trace(isTrue!=1), the result is false because for normal equality isTrue is considered as 1. But if we write trace(isTrue!==1), the result is true because for explicit equality isTrue is not equal to 1. Therefore, this satisfies the explicit inequality condition and hence, we can expect the result as true.

Don't get confused with these four different types of operators. To sum up, you can remember the following points and avoid confusion. == and != checks for equality and inequality respectively but consider the closest value as well during comparison. === and !== checks for explicit equality and explicit inequality respectively and the comparison requires the values to be absolutely equal or absolutely unequal.

Video Transcript

00:00Now, what if we wanted to be more strict with our traces?
00:03What if we wanted a way to be able to ask,
00:06is this value really explicitly equal to that value?
00:11Is true really, really equal n?
00:15And the way we do that is equals and another equal.
00:19So three times equal is basically
00:21asking from Flash not to convert the values automatically,
00:26to check them on their current types
00:29and see what value they are.
00:31So if we trace this time around n is true,
00:34but instead of that typing three times equal and then is true,
00:41let's uncomment the last one, we're
00:44going to see that n three times equal true would come back
00:48as false, because it would only become true if the values are
00:54completely the same, which are the same type as well.
01:00And we could see that the first one returns false
01:03as we expected.
01:05And the second item returns true as we expected.
01:08Let's just practice this a little bit more
01:10before we move on to the next topic,
01:12because it's a bit, might be a little bit,
01:16might be a little bit too much.
01:18But let's try one more time.
01:20So let's try and checking again.
01:21So our is true is a Boolean, is true.
01:25And let's ask it if it equals 1.
01:29And if we ask it if it equals 1, Flash automatically
01:32is going to convert is true to a number,
01:36and it's going to convert it into 1.
01:38So by default, the answer would be true.
01:43Well, if we wanted to compare is true to be 100% equals,
01:53without any conversion, the value would come back as false.
01:59Now, if we really wanted our equation to come back as true,
02:02what is the only possible value?
02:04Is true?
02:05First, is true equal to is true?
02:07That would be absolutely true, because it equals itself.
02:15Not only that it's true, not only that it's true,
02:18what we're actually asking is true equals true.
02:21We're not really asking is true equals is true,
02:24because we're working with primitives.
02:27And so far, all of our primitive,
02:29whenever we check an equation of something,
02:31whenever we use something, we're actually extracting the data
02:34from that variable.
02:35So it's actually the exact same thing
02:38as tracing out true equals true, which is true.
02:46And we can see that our first trace is true,
02:48because obviously true is explicitly equal to true.
02:52Well, if we try to trace true equals 1, that's not explicit,
02:58which is the exact same thing as what
02:59we've done in this line, only instead of using a variable,
03:03putting it directly, but it's the exact same thing.
03:06So our first true, our first trace should come out as false.
03:11So we see now, so now we know how to work
03:14with explicit, explicit equation,
03:17making sure that it's only if it's completely, completely,
03:20accurately true and without any conversions.
03:23Now, same thing goes to not equals.
03:26So so far, we've worked with not equals
03:29on a non-explicit way.
03:31But we could use the not equals equals.
03:35And not equals equals is the same thing
03:39as it's basically a strict not equals.
03:44So let's see what that means.
03:45So if we trace out does true not equal 1, or more accurately,
03:54not equal 0, because true equals 1.
03:59But is the statement true not equal 0 a valid statement?
04:03It absolutely is, because true is converted to 1.
04:07And then we're basically asking, is 1 not equal 0?
04:10Obviously, it's not.
04:12So we'll come back as true.
04:17But what if we didn't want this conversion to happen?
04:19If we didn't want this conversion to happen,
04:21we could have just said true not equals equals 0.
04:28And that would come back as false.
04:30Because true does not equal 0 if we're explicitly
04:35checking it between the two of them.
04:54Basically, the reason why this is coming out as true
04:58is because that does not equal that,
05:01and that is absolutely true.
05:04True does not equal 0 at any given time.
05:10Well, why does this come back as true?
05:13Well, why does this come back as false?
05:15What comes back as false is because these two items
05:17do equal each other.
05:19Basically, the reason why this comes back as false
05:22is because why this one came back as true
05:26is because if asked if they were explicitly equal,
05:31the value would be true.
05:33Sorry, the value would be false.
05:36Thus, automatically, if we were asking it not,
05:38it would be not true.
05:39So if we break it into a few steps,
05:42and we just say not everything inside of here, true equals 0,
05:49does this value, the value inside of here, true?
05:53No, the value inside of here is false, right?
05:56So we're saying not false.
05:58If it's not false, it must be true.
06:03So those are the steps that actually
06:04created the value of true.
06:06Well, on the other hand, right here,
06:09if we would ask not true equals equals true,
06:16the value that we should get back is that not true?
06:20No, it is true.
06:22So that would be true.
06:24And because it's true and we're asking not whatever
06:26comes back, then the value would be false.
06:30So let's see another option now that we get it.
06:33Let's try another trace right here.
06:35Let's try to trace and ask, is true not equals equals 1?
06:43What value do you think would come back?
06:45Let's take it out to the test.
06:46So let's take out our not away for a second.
06:49And let's just focus on the assumption
06:51that it was completely true.
06:52So if true equals equals 1, is that true?
06:57No, because it would be true if there
06:58would be only one equation.
06:59So that would come back as false,
07:03which we want the opposite of whatever
07:04comes back, which would be true.
07:07So this value should come back as true.
07:12And it does.
07:14OK, so let's have a quick recap of the type of equality
07:21operators that we've met.
07:22We've met the equals.
07:24We've met the not equals.
07:27We've met the explicitly equal.
07:30And we've met the explicitly not equal.
07:33And by the way, this is one of the most uncommon things
07:37that you will use.
07:37But it's definitely important to know.
07:40It's a very important thing to kind of grasp and get used
07:43to all these conditionals in your head.
07:45But don't worry if you don't feel very comfortable with it.
07:49Because no one really feels, well,
07:50maybe there's a few people that feel very
07:52comfortable with an explicit opposite.
07:54But it's fine.
07:55As long as you understand the concept,
07:58as long as if we break it down and you could see the path,
08:02then you're in a good place.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!