JavaScript is able to compare two values. Try evaluating this expression in the editor:
1 === 2
The result is false
, because 1
does not equal 2
.
Try these expressions instead:
1 === 1
1 < 2
Both of the above expressions resolve to true
, as 1
is equal to 1
and less than 2
.
Operators
We can use these operators to compare values in JavaScript:
Operator | Comparison |
---|---|
=== |
Strictly Equal |
!== |
Not Strictly Equal |
== |
Equal |
!= |
Not Equal |
> |
Greater Than |
>= |
Greater Than or Equal To |
< |
Less Than |
<= |
Less Than or Equal To |
"Equals" Gotcha
Be careful when typing the equals operators ==
and ===
.
In math equations, we are used to representing "equals" with a single equals sign. In JavaScript, we will learn to use the single equals sign to set values. It has a completely different purpose.
We need to use the comparison operators listed above to compare values. If you try to use a single equals sign to compare values, your code won't work as expected and will probably throw an error.