###JavaScript has both strict and type-converting comparison.
For strict equality the objects being compared must have the same type, and:
- Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
- Two numbers are strictly equal when they are numerically equal (have the same number value).
- NaN is not equal to anything, including NaN.
- Positive and negative zeros are equal.
- Boolean operands are strictly equal if both are true or both are false.
- Objects are strictly equal only if they refer to the same Object instance.
- Null and Undefined are ==, but not === (they're not of the same type, because they have no real type - they're absence).
For type-converting equality:
- Whitespace, empty strings and zeroes are considered falsey.
- Undefined and null are both non-values (neither true nor false).
- A string with any value other than whitespace, zero, or empty is considered truey.
- After these type-conversions have been made, follow the same rules as strict.
Examples:
- '' == '0' // false, they're both treated as strings (note the quotes!) - this isn't a cross-type comparison.
- 0 == '' // true, the string has a zero/falsey value (whitespace), and zero is falsey.
- 0 == '0' // true, the zero here is considered falsey, same as whitespace.
- false == 'false' // false, the string has a non-zero/non-falsey value.
- false == '0' // true, for the same reason as 3.
- false == undefined // false, undefined is absence, nothing, a non-value.
- false == null // false, for the same reason as 6.
- null == undefined // true, they're both non-values.
- ' \t\r\n ' == 0 // true, again whitespace is considered falsey.
Sources: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons http://stackoverflow.com/questions/523643/difference-between-and-in-javascript