Yet more JavaScript notes. This time, it’s all about equality. They key part to various control structures (e.g., if
, switch
, while
, do
and for
)in JavaScript (and other languages for that matter), are the comparison operators.
These are less than, greater than, equals and the combinations thereof. In JavaScript, the operands (the things that are being compared) get converted automatically. So, you need to be careful and favor the strict comparisons.
=== strictly equal to
!== not strictly equal to
The strict equality comparisons treat different types as unequal which is probably what you want (e.g. the string ‘4’ is not strictly equal to the integer 4). undefined
and null
are only strictly equal to themselves.
Using the strict equality comparisons help comply with Golden Rule #3: Know your types and avoid automatic type conversion.