var foo=[0];
if(foo) alert('first');
if(foo==true) alert('second');
Tell me please, why the second alert doesn't work? In first alert foo casts to Boolean, so
Boolean(foo);
//true
If "foo" is "true", why doesn't the second alert work?
Because initially foo is an Array, not a Boolean and you are comparing foo to a boolean value. The if (...)-statement evaluates to true or false and foo == true evaluates to false here. If you used if (!!foo == true) (or just if (!!foo)) or if (foo != null) or if (foo) the second alert would have fired.
Because there's a difference between the conversion of foo to boolean (which works for an Array) and the comparison of foo to true.
In the latter case, it's a comparison without a conversion, and foo clearly is not the same as true. Note that a conversion does still take place: foo == true is false which is finally "converted" to false for the if. :)
=== is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type. == is the equal operator and returns a boolean true if both the operands are equal... which they're still not.[1]==true;//true, wow! and [0]==true;//false and [0]==false;//true, but [2]==true;//false and [2]==false;//false. I understand how it works, now, when i read ECMA-262 (ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf), but i still don't understand algorithm of if statement expression check.
true?