Essential JavaScript
Introduction
JavaScript Basics
Numbers
Strings
Logic
Variables
Loops
Objects
Arrays
Going Further
Essential JavaScript
Introduction
JavaScript Basics
Numbers
Strings
Logic
Variables
Loops
Objects
Arrays
Going Further
Comparison Operators
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.
Learning Goals
I can compare two values in JavaScript
I know what type of value is returned when I use a comparison operator
I know that I need at least two equals signs to check if values are equal
Code Editor
Click "Run Code" to execute your JavaScript in a secure sandbox and see the output below.
Console Output
0 output lines