Is this normal? Is it a feature or a bug? (I’m using firebug):
>>> '' || true
true
>>> '' || false
false
>>> '' && false
""
>>> '' && true
""
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is not converting the empty string to
Boolean.With
||It is evaluating the left hand side, of which an empty string is falsy. It then checks the right hand side (because it’s an or, and that side may be true), and returns that value.
With
&&Because
&&needs both sides to be true and the left hand side is falsy, it doesn’t bother checking the right hand side (short circuit evaluation). Therefore, it just returns the left hand side, which is the empty string.JavaScript always returns the last value it evaluated.