Comparison operators are used in logical statements to determine whether two variables or objects are equal by some already set or custom defined criteria. If the two variables or objects are deduced to be equal the operator returns
false if they aren't.
<- less than
>- greater than
<=- less or equal than
>=- greater or equal than
===- equal to (loose and strict equality operator)
!==- not equal to (loose and strict inequality operator)
In this article, we will explain the differences between loose and strict equality operators and go over some examples of their usage.
The process of converting a value from one type to another is called type coercion. Type coercion can be explicit and implicit.
// This would result in x being a number 42 let x = Number("42"); // Similarly,this would result in y being cast to the boolean value false let y = Boolean(0);
// x is going to be cast to the string "1". let x = 1 + "";
The expression should be evaluated and assigned to
x. The first number is
+operator besides it. This implies integer addition with the next number. However, there's an empty string,
"", there. This can't be converted into a number. However, we can also concatenate strings with
+, so let's match them as strings and perform concatenation. Finally, the result is returned and assigned to
+ operator, which in this case is a string.
/* In this case, the JS engine deduces that both the operands and the result have to be cast to an integer because it's the only type that has a defined subtraction operation (-) */ let y = "5" - "2"; // Because booleans don't have a + operation, the JS engine converts both true values into 1's let z = true + true;
Now, it's exactly this type of coercion that the
== operator uses to check for equality. Let's go over some examples of loose equality operator usage, in the Node REPL:
42 == "42" true "42" == '42' true
'42' are all matched into the same data type, and are all loosly equal, returning
true in the comparisons.
Booleans follow the same principle:
true == 1 true true + true == 2 true false == 0 true false + true == 1 true
0. We can implicitly convert these into integers by adding them.
Finally, here's an example of an (unreadable) but syntaxically valid comparison:
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
("42" == '42') + (42 == "42") == 2 true
"42" == '42' is evaluated as
true, as well as
42 == "42". This side of the expression therefore also evaluated to
true + true, which is implicitly converted into
1+1. That's obviously
2, so the expression returns
A falsy value is a variable which evaluates to
There are a couple of simple rules when it comes to using the double equals operator on falsy values:
truewhen compared to themselves or each other, and
falsewhen compared to anything else
NaNcompared to anything (even another
So, these would all be
> false == 0; true > false == ""; true > 0 == ""; true > undefined == null; true > undefined == undefined; true > null == null; true >  == false; true >  == 0; true
While these would all be
> undefined == false; false > 0 == null; false > null == true false > NaN == false; false > NaN == NaN; false
true unless both the type and values of the two expressions match. They aren't matched on type anymore.
Now, let's have a look at some strict equality operator examples:
true === true true true === 1 false false === 0 false
This example produces
true in the first case because both the type (boolean) and value (
true) are equal. In the other two cases, it returns
false, because although the values are the same, the types don't match. We're comparing a boolean to an integer.
42 === 42 true 42 === "42" false
Here, both the number and value are equal in the first example, resulting in
true, while the value and type mismatch in the second example, resulting in
We also explained what type coercion was, and the concept of explicit and implicit type coercion.
By using the strict equality operator you are testing both the type and value, and you can therefore be sure to always execute a true equality test.