Let’s take a look at the details:
But let’s take a look at primitive types:
- If either operand is number – convert everything to numbers and perform strict comparison (identity operator);
- If either operand is string – convert everything to strings and perform strict comparison (identity operator);
- If both operands are objects – perform reference comparison.
So, let’s take a close look at the “strange” behavior from the code sample:
- console.log(a == c). In this line “a” and “c” both are strings, no conversion required, but this strings are not equal, so result is false.
Let’s replace equals with identity:
I hope – everything is clear now: no any conversion, code clean and easy to read. One more word about equal sign:
Probably someone will say: “why should I write”:
But is it a real problem to write a dozen of characters to make your code readable? Sometimes you cannot write just:
myValue can be 0 or empty string and it will cause unexpected behavior.