When a conditional statement is executed in JavaScript, it returns the last evaluated value.
var x = ""; alert(x && x.length > 0);
The empty string is false, so when you use only x in state, it will be false. Since you are using && , if the LHS is false, then there is no reason to worry about checking the RHS. This is a short circuit rating . Therefore, the last evaluated part, the empty string, is returned in alert() .
var y = "abc"; alert(y && y.length > 0);
A non-empty string is true. Thus, LHS is true, and since it is && , it is evaluated by RHS (it needs to know if the whole condition is true). The return value y.length > 0 is true , so it is passed to your alert() .
alex
source share