Possible Duplicate:
Javascript conditional order evaluation
How do multiple conditions in an if-statement pan out?
if( bacon && bacon == "crispy") {
self.eat(bacon);
}
If bacon doesn’t exist, will it still try to check if bacon is “crispy”?
No, it uses short-circuit evaluation, so if evaluating the LHS means it doesn’t need to evaluate the RHS, it won’t.
This is exploited in JavaScript quite frequently…
If
foois truthy (the LHS), then the condition is known to betrueandfoowill be assigned its value (these conditions return the last evaluated expression).However, if
foois falsy, it will evaluate the RHS expression, and then assign that result tofoo.