Here’s a quick example that demonstrates these two types of operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The key point is noting that while
a == c is true,
a === c is false.
Why? When using the
== operator the string
"4" is type cast into a
which is compared to
4 is equivalent to
4, this evaluates to true.
However, when using the
=== operator the string
"4" is directly compared to the
4. This fails because a
String is not equivalent to a
So, if you’re ever trying to figure out why a comparison is evaluating to true when it should be evaluating to false, double check that you’re not accidentally type casting one of the operands.
Want to dive deeper? Check out these articles for even more information: