Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Quite often comparing two numbers is necessary in our programs. JavaScript provides a handful of comparison operators that make comparing numbers very easy.

0:00
[? Music ?] [Treehouse]

0:08
Often in our program we need to compare 2 numbers against each other.

0:11
Let's see how we do that in JavaScript.

0:14
We've seen the various ways to create numbers

0:16
as well as create other numbers by using mathematical operations

0:20
like addition, subtraction, multiplication, division, and modulo.

0:24
And we saw how the order of operations works

0:26
and how parentheses can be used to control

0:28
the order of the operations in an expression.

0:31
Now in strings, we saw that we could compare 2 strings together

0:35
using less than, greater than, as well as less than or equal to,

0:39
greater than or equal to, and the equal to operator.

0:43
And numbers, unsurprisingly, have the same type of comparison,

0:48
so if we were to, for instance, check out console.log,

0:52
and let's just take the expression 1 < 2,

0:57
we would expect this to return true

1:00
because 1 is in fact less than 2.

1:03
Now, we had another console.log up here, and I'm just going to

1:06
comment it out so we only get 1 console here.

1:10
We can see 1 < 2 is our only console.log,

1:13
so if we refresh, we see it's true.

1:17
We also have the greater than operator, so this expression means

1:21
1 > 2, which is obviously false,

1:26
and we have the greater than or equal to and less than or equal to operators,

1:30
which are greater than then the equal operator.

1:35
Now, is 1 greater than or equal to 2?

1:37
If we refresh, we see false,

1:39
but is 1 greater than or equal to 1?

1:43
We see true.

1:45
If we didn't have this equal sign here, and we said 1 is greater than 1,

1:48
well, that's false, and this works in the other direction.

1:52
We can say 1 is less than or equal to 1,

1:55
which would obviously be true.

1:57
And 1 is less than 1 would be false,

2:01
and this works even when our representations are slightly different.

2:06
For instance, if we had 1.0 on one side and just 1 on the other,

2:11
and we said less than or equal to,

2:14
well, 1 is equal to 1.0, so less than,

2:18
greater than, as well as less than and equal to and greater than and equal to

2:21
are pretty easy to use.

2:24
We've seen the equality operator of 3 equal signs,

2:26
so 1 is equal to 1.0.

2:30
And we refresh. We see true.

2:33
And one is equal to 1.5,

2:37
of course, is false.

2:40
You'll notice I'm using the triple equals operator, which we've seen before,

2:44
and I explained that there's also a double equals operator.

2:48
And the reason we don't use this is because if we compare

2:51
a number to a string, we'll get some interesting results.

2:54
For instance, if we checked the number 1 is equal to the string 1

2:59
with 2 equal operators, we'll see it's true.

3:03
However, if we do 3 equals, we'll see that it's false,

3:07
and this is because a triple equal operator

3:09
will not just check that the values are the same

3:11
but they are the same type.

3:13
Now, the double equal operator has a lot of weird edge cases

3:16
where it tries to convert numbers to strings or to other types

3:20
where you may get some pretty unusual values when comparing

3:23
the equality of 2 different types of objects.

3:26
That's why any time we're doing a comparison like this

3:28
we want to use a triple equals operator and make sure

3:32
we're working with the same types.

3:34
In general practice, we don't want to compare a number to a string.

3:38
Instead, we may want to do something like parseInt of that string.

3:43
And remember to add our second argument 10 here so it's in base 10,

3:48
and then we would get true.

3:51
That way we're being very explicit about what we're checking against.

3:54
Finally, the opposite of the triple equals operator

3:58
is the bang equals equals operator.

4:01
This exclamation point, or bang, means not in JavaScript,

4:05
which means 1 is not equal to 1 in this case,

4:09
so does 1 not equal 1?

4:13
No, so that would be false.

4:17
So, for instance, if we checked 1 is not equal to 3,

4:22
this would be true.

4:24
Again, if we just used an exclamation point

4:27
and 1 equals, this would be the opposite of equals equals,

4:32
while the exclamation point with 2 equals is the opposite of 3 equals.

4:38
These are the ways we can compare our numbers.

4:41
We can place this expression in an if statement, for instance,

4:44
to control the flow of our program based on the equality

4:48
or inequality of an expression of numbers like this.

4:54
"if (1 < 2) console.log("Yes"),"

5:03
and we could use "else console.log("No")."

5:10
And this is just like we did with strings.

5:12
We just simply put our comparison operator inside the if statement here,

5:16
and it will branch based on the truth of that inequality or equality.

5:20
We can check it out, and we can see, of course,

5:22
yes is the branch that actually gets run.

5:26
JavaScript provides us with a pretty large library of mathematical functions

5:30
and constants we can use when we're doing math in JavaScript.

5:33
Up next, we'll take a look at where we can find those functions.

5:37
[? Music ?] [Treehouse]
You need to sign up for Treehouse in order to download course files.
Sign up