The &&
operator returns 0
for the expression 1 && 0
because its semantics are different than those of the same operator (well, symbolically the same) in other C-like languages.
In Javascript, the &&
operator does coerce its operands to boolean values, but only for the purposes of evaluation. The result of an expression of the form
e1 && e2 && e3 ...
is the actual value of the first subexpression en
whose coerced boolean value is false
. If they're all true
when coerced to boolean, then the result is the actual value of the last en
. Similarly, the ||
operator interprets an expression like this:
e1 || e2 || e3 ...
by returning the actual value of the first en
whose coerced boolean value is true
. If they're all false, then the value is the actual value of the last one.
Implicit in those descriptions is the fact that both &&
and ||
stop evaluating the subexpressions as soon as their conditions for completion are met.