Possible Duplicate:
Why does (‘0’ ? ‘a’ : ‘b’) behave different than (‘0’ == true ? ‘a’ : ‘b’)
I'm currently learning Javascript and so far am pleased about the language.
This morning, when writing a test, I realized something I can't explain:
When evaluating boolean expressions, we have:
// a && b returns b in case a && b are evaluated to true: OK
'2' && '3' => '3'
'3' && '2' => '2'
// a && b returns a in case a is evaluated to false, b otherwise: OK
false && '' => false
'' && false => ''
// '' and '0' are evaluated to false in a boolean context: OK
'' == false => true
'0' == false => true
// Here is the "problem":
'' && '0' => '' // To be expected: both operands evaluate to false
'0' && '' => '' // What ?!!
So my question is simple: why does '0' && ''
return ''
?
I would expect both operands to evaluate to false but it seems in that context '0'
does not. Why is so ?