Javascript apparent madness [closed]

sheenxd

New Member
\[quote\] Possible Duplicate:
Conflicting boolean values of an empty JavaScript array \[/quote\]What is the rationale behind the fact that\[code\][ ([] == false), ([] ? 1 : 2) ]\[/code\]returns \[code\][true, 1]\[/code\]?In other words an empty list is logically true in a boolean context, but is equal to \[code\]false\[/code\].I know that using \[code\]===\[/code\] solves the issue, but what is the explanation behind this apparently totally illogical choice?In other words is this considered a mistake in the language, something unintentional that just happened and that cannot be fixed because it's too late or really in the design of the language someone thought it was cool to have this kind of apparent madness that I'm sure is quite confusing for many programmers?The technical explanation of how this happens is at the same time amazing and scaring, I was however more interested in what is behind this design.EditI accepted very detailed Nick Retallack explanation, even if it's only about the technical reasons of why \[code\][]==false\[/code\] is true: surprisingly enough it happens is because \[code\][]\[/code\] converted to string is an empty string and the empty string numeric value is special cased to be 0 instead of the apparently more logical \[code\]NaN\[/code\]. With an empty object for example the comparison \[code\]({}) == false\[/code\] returns false because the string representation of an empty object is not the empty string.My curiosity still remains about all this being just unanticipated (and now unfortunately solidified in a standard).
 
Top