Written by Preston Lamb
Posted on Apr 6, 2020
Make sure to check out the other posts in this series:
- Nullish Coalescing Operator
true when you were expecting
console.log(1 == '1')/ // true
I have had this happen to me before, and at first was really confused. But once I realized what was going on, it made more sense. Let's talk about why this happens.
number and converts it to a
string) and then does the comparison. That's why the statement
1 == '1' is
true. Obviously a number is not the same as a string, but with abstract comparison they are compared to each other as if they were the same. Make sense? The same is true for
!=. If we take the inverse of the previous statement:
console.log(1 != '1'); // false
We see that the result of this comparison is
if statements or why variables were having values set to what I thought was the incorrect value. Understanding what was happening in the comparison helped me to clear everything up.
Another situation to be aware of when using abstract comparison is when you are comparing
console.log(null == undefined); // true
This is arguably a bigger issue than comparing strings and numbers. There are times where you want to do a specific thing if the value of the variable is
null and a different thing if it's
undefined. They have different implied meanings, even though they are both the absence of a value.
So how do we ensure that
1 does not equal
'1'? That's where strict comparisons come in to play. With strict comparisons, no type conversion happens, and the values are compared to each other exactly as you wrote them. So let's look at our examples from above and see what their results will be with strict comparisons:
console.log(1 === '1'); // false console.log(1 !== '1'); // true console.log(null === undefined); // false
Likely when you make a comparison between two objects, you actually want to know if two values are strictly equal or strictly not equal to each other. I always use the strict comparison, and recommend that everyone do the same. There might be some edge cases where you need abstract comparisons only for equality, but I have found very few cases where I needed them over strict comparisons.
There are really two types of comparison operators: equality/inequality operators (which we've really covered so far) and relational operators. Let's go over these in a little more depth. We'll start with equality/inequality operators and then cover relational operators.
We've talked a lot about equality/inequality operators already. The four equality/inequality operators are
!==. In this section, we'll just give a few notes on these comparisons. We've already shown several examples of using these comparison operators.
numbersare abstractly or strictly equal to each other if they are the exact same number
stringsare abstractly or strictly equal to each other if they have the same sequence of characters in the same order
objectsare abstractly or strictly equal to each other only if they point to the same location in memory
booleanvalues are only equal to each other if they are both
nullare abstractly equal to each other
The inverse of the above rules apply to using the inequality operators (
Relational operators are the following operators:
<=. You'll likely recognize these from your math classes in school. They are pretty straight forward, but let's go over each of them real quick.
For the greater than (
>) operator, the result is true if the left side of the operator is a greater value than the right side. Some examples are
console.log(4 > 1); // true console.log(4 > 4); // false console.log(4 > 8); // false
For the greater than or equal to (
>=) operator, the result is true if the left side of the operator is a greater value or equal to the right side. Some examples are
console.log(4 >= 4); // true console.log(4 >= 8); // false
For the less than (
<) operator, the result is true if the left side of the operator is a lesser value than the right side. Some examples are
console.log(4 < 8); // true console.log(4 < 4); // false console.log(4 < 2); // false
For the less than or equal to (
<=) operator, the result is true if the left side of the operator is a lesser value or equal to the right side. Some examples are
console.log(4 <= 4); // true console.log(4 <= 2); // false