The community is working on translating this tutorial into Chinese, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
Comparison operators
We just discussed the assignment operator, which is a single equality sign (=). However, by adding a secondary equality sign (==), you completely change the behavior - instead of an assignment, you are now performing a comparison. If you're new to programming, this can sometimes be difficult to maneuver in, but you have to get used to it. In fact, with JavaScript, there's even a triple equality sign operator ( === ) we have to deal with, but more on that later.
Let's quickly run through all the comparison operators, to know what they're called and how they work.
Equality operator: ==
We'll start with the equality operator, which compares two values. When using this comparison operator, as well as most of the other ones, the result is a boolean - either the comparison is true or false. Therefore, you will often use them in combination with an if statement, like this:
let n1 = prompt("Please input a number");
if(n1 == 42)
alert("Correct answer!");
Notice how we use the assignment operator (=) on the first line, and then the equality comparison operator (==) on the next line. The visual difference is subtle, but make sure to learn the difference - it's very important.
Inequality operator: !=
But, what if you need to ask whether something is NOT the same? No worries, you can negate the equality operator using an exclamation point, to mean the exact opposite. Here's an example:
let n1 = prompt("Please input a number");
if(n1 != 42)
alert("Wrong answer - it should be 42!");
Notice how I simply changed == to != to negate the condition.
Greater than/less than: > and <
Sometimes, you need to comparison not just about equality, but about size. For this, JavaScript comes with greater-than and less-than operators - they will let you know whether a value is greater than or less than another value. Let's illustrate this with an example:
let n1 = prompt("Please input a number between 1 and 10");
if(n1 < 1)
alert("Too low!");
if(n1 > 10)
alert("Too high!");
Greater/less than or equal to: >= and <=
Sometimes you need to know whether a value is greater than or equal to a value (or smaller). JavaScript comes with a couple of operators for that as well - let's modify the above example to use them:
let n1 = prompt("Please input a number between 1 and 10");
if(n1 <= 0)
alert("Too low!");
if(n1 >= 11)
alert("Too high!");
Strict equality: ===
I teased a bit about this in the beginning of the article: There's a more strict comparison operator, to do the same thing as we just did above, but in a more strict way. But what is all this strictness about and why do we need it?
We talked a bit about this in the chapter about data types, but it's time to revisit the subject. Because of the loose and dynamic type system in JavaScript, the interpreter will do things behind the scenes to make things easier for you. For instance, you may have a variable which is basically a text string, but containing a number. So, is "42" the same as 42? In JavaScript, it is:
let n1 = "42", n2 = 42;
if(n1 == n2)
alert("A match!");
However, if you come from a programming language with strong typing, this will seem odd to you. And there WILL be situations where you need to be more strict about types, even in JavaScript as well. For these situations, simply use the strict equality operator, by changing two equal signs to three:
let n1 = "42", n2 = 42;
if(n1 === n2)
alert("A match!");
else
alert("Sorry, no strict match!");
What happens here is actually quite simple: Because we use the strict equality operator, JavaScript is NOT performing any type conversion in the background when comparing the two values. Instead, it takes a quick look at them and immediately realizes that they are not of the same type and therefore can't be truly equal.
Strict inequality: !==
And of course, you can check for strict inequality the same way, by changing one of the three equal signs into an exclamation point:
let n1 = "42", n2 = 42;
if(n1 !== n2)
alert("Sorry, no strict match!");
else
alert("A match!");
Summary
Comparing values is an essential part of most programming tasks, and hopefully you now understand how the various comparison operators work in JavaScript. If you're unsure, give this article another read, because you will definitely need a strong grasp of the way they work when you move on to more complicated subjects.