Equality vs Strict Equality
Equality ==
- Compares values for equality if types of values are same. For example
- If type of values are different then values is first converted and than compared. Javascript tries to convert the operand to the same type before comparision.
In the above case value '1' is converted to number and than compared, even '1' is of string type and 1 is of number comparison between two return true.
- For Objects and Array there reference is checked if they are same, there values are not compared.
Strict Equality ===
- Strict Equality checks types and does not convert the operand if types are different. If types are different it returns false.
If types are same than values is compared.
In case of Arrays and Objects reference is compared
In the above case obj1 and obj2 share the same reference true is returned.
Same comparison happens for arrays.
Some Exceptions which are important to note
- Comparison between NaN with anything, including itself, using equality operators will always return false. This is because NaN is considered not equal to any value, even itself.
- Comparing Null and Undefined will be equal with equality comparison, all other comparison between these two returns false.
- Empty String is equal to false in equality comparison.
- When comparing Arrays and Objects always remember that they are compared by reference and not by their values.