From the course: Learning the JavaScript Language

Simple comparisons

- [Instructor] In this video, we're going to look at simple comparisons, which are the beginning of the way that we can ask the computer a question about something and get back a meaningful answer. Going to maximize the terminal here to give us as much room as we can possibly have. The first one we'll look at is equality. That is knowing whether something is the same as something else. We'll start out really easy with this and I'll create a couple of variables here. We'll create a variable called one, which will contain the number one and another variable called two, which will contain the number two. We can just type these out to make sure that they have the values we expect. Most of the time at least one of the items will be asking a question about will be a variable. So having these two available is helpful. First, we're going to ask if one and one are the same. The way I'm going to do that is using an operator made of three equal signs, one after another, 1, 2, 3. This operator is called the strict equality operator and it tests whether the object on the left is identical to the one on the right. And as we can see here, the answer is true. There's a similar operator that asks whether this item on the left is not identical to the one on the right, that's called strict inequality. And to do that, we just substitute that first equal sign for an exclamation mark, which programmers also call a bang. So in this case we're asking is one not identical to one? We would expect the answer to be false, and indeed it is. Now, if we change that second one to our variable two, and because we're asking here are these two things strictly not the same, we get back the answer true. And then finally we'll change that identical, that not identical back to the strict equality operator again. And our answer is false. So at this point we've started to ask some questions and started to get back some answers. These kind of comparisons are all over the place in most programs. You might be asking, is this page's URL about dot HTML or is this button that was just clicked the main menu button? Checking whether something is equal to something else is a comparison you'll just be doing a lot. There's another pair of operators that are similar, but act differently. You'll see them a lot in programs out in the wild. These are simply the equality and inequality operators with only two equal signs or one bang and one equal sign. These also check equality, but in a way that's not as strict hence the difference in names. So let's try them out. First, we'll do a regular equality check on the variables one with itself and that still comes back as true because they are the same. After this, things get a little weird. So let's try one. And for the thing that we will compare it to let's create a string with the number one in the string. So it's a string, but it has one inside it. And in terms of this kind of not strict equality, the answer is true. Now, strictly they are different, but this operator sort of implicitly converts the data types from string to number in this case and says, "Yeah, sure, they're pretty much the same." The inequality operator does the same thing for inequality, so we get the opposite of the previous weird result. It is generally recommended that you use the strict versions of these operators for the most predictable results in your code, but people do still often use the non-strict versions as well. The particulars of which kinds of values will return true or false for the equality and inequality operator are a little bit too detailed to go into here, especially for people who are just starting out. You can look at the equality operators section of the operators page on the Mozilla developer network for more information on this. JavaScript also has relational operators, which are straight out of your math class. So for example, we could ask is one less than two? Of course it is. And so that returns true. Then we can ask is one greater than two just by flipping that angle bracket. And indeed that's false. We can also combine greater or less than with an equal sign. Less than or equal to is the same angle bracket as we saw above directly followed by an equal sign. So now we're asking is one less than or equal to two? And indeed it is. So that returns true. Likewise, we can ask is one less than or equal to one? The answer is true because indeed one is equal to one. Of course we can ask is one greater than or equal to two? The answer of course is no. So that returns false. And of course we don't have to use variables. Our final question will be is 10 greater than or equal to two, just the number 10. And of course that is true. So that concludes our first look at comparisons, the various equality operators, and the relational operators. Expect to use all of these early and often.

Contents