This is due to the type of coercion of the == operator (equality).
An empty array is considered true (as an empty object), so a second warning is raised.
However, if you use ([] == false) , your array is forced to its string representation *, which is a "" , which is then considered a false value , which makes the condition true, thereby also triggering the first warning.
If you want to avoid type coercion, you should use the === (identity) operator, which is preferred, and the famous Douglas Crockford advanced comparison method in javascript.
You can read more about this in this comprehensive answer .
* ( Object.prototype.toString is called on it)
EDIT: Fun with JS Comparison:
NaN == false // false NaN == true // also false NaN == NaN // false if(NaN) // false if(!NaN) // true 0 == '0' // true '' == 0 // true '' == '0' // false !
This shows the real "strength" of comparing with == due to the weird rules mentioned in the bfavarettos answer.
Christoph Dec 10 '12 at 11:00 2012-12-10 11:00
source share