3.5 The Equality Operators
In the last video you learned about the comparison operator, specifically the relational and equality operators. Now technically in JavaScript, we have another equality operator that is indicated by two equal signs instead of 3. So you might be wondering what the difference is? Well first of all, what we have on top is what we call a strict equality operator, and what we have on the bottom is what we call a simple equality operator. Now this is a strict equality operator guaranteed that it's two values that we have on the sides of this operator have the same type and the same value. What do I mean by that? Well in this example, on both sides of this operator we have two numbers, so their type and value are equal so if you save the changes we get true and true. Now let's take a look at another example, this time I'm going to change one of these numbers to a string. Now if I save the changes, this expression will become false because here we are comparing a string to a number. So the types here don't match which is why we get false. The strict equality operator guarantees that these two numbers are of the same type and value. Now the simple equality operator behaves differently so we'll comment on these two lines to focus on this operator. Here we compare 1 to 1. we get true. Now what happens if we compare 1 as a string with 1 as a number. We should get true, but in the previous example we got false, so let's see what happens here. In this case this operator looks at the value on the left side. Here we have a string so it will automatically convert what we have on the right side into a string, so it will look like this. And now because these values are exactly the same, we get true on the console. Now what happens on the left side we have a boolean? Well this operator looks at what we have on the left side and because it's a boolean it will automatically convert the value on the right side to a boolean so it's like comparing true with true. And if you save the changes again we get true. So here's what you need to take away, the strict equality operator guarantees that both values have the same type and value. The simple equality operator doesn't care about matching types, if the types don't match, it will convert the type of what we have on the right side to match what we have on the left side, and then it will only check if the values are equal now most of the time we should use the strict equality operator because it's more accurate and fair. That's it for this little demonstration on equality operators, we'll see you on the next demonstration where we'll talk about ternary operators.