the coerce equality operator (==)
it is perfectly legal.
Type Coercion usually results in unlogical situations.
the strict equality operator (===)
This operator is much more strict, it will compare the value and the type of variables.
The strict equality operator does not perform type coercion so it behaves in a more logical way
You can also use it to compare objects, but remember that the objects must be the same a new instance of the same object will not pass the comparison
So it is highly recommended that you only use the strict comparison operator, if you need to coerce types you should do it explicitly and never leave it to the interpreter
the order matters
The order of the comparison terms matters, you can avoid lots of weird bugs by following a simple rule
Whenever possible always put scalars/primitive types on the left of the comparison
Can you spot the error?
This typo occurs very often, it is easy to miss a
= and instead of comparing you assign a value.
You have no idea you missed the
=, the expression always evaluates to true and finding the error can take a long time.
if instead you place the primitive type on the left side of the comparison, you will get an error thrown, making debuging extremely simple and fast.