Java Reference
In-Depth Information
Following is a truth table of possible evaluations of left-hand sides and right-hand sides and the result
when AND is used.
Left-Hand Side
Right-Hand Side
Result
true
true
true
false
true
false
true
false
false
false
false
false
Although the table is, strictly speaking, true, it's worth noting that JavaScript doesn't like doing unnec-
essary work. Well, who does! If the left-hand side is false, even if the right-hand side does evaluate to
true, it won't make any difference to the fi nal result — it'll still be false. So to avoid wasting time, if
the left-hand side is false, JavaScript doesn't even bother checking the right-hand side and just returns
a result of false.
OR
Just like AND, OR also works much as it does in English. For example, you might say that if it is raining or
if it is snowing, then you'll take an umbrella. If either of the conditions “it is raining” or “it is snowing”
is true, you will take an umbrella.
Again, just like AND, the OR operator acts on two Boolean values (one from its left-hand side and one
from its right-hand side) and returns another Boolean value. If the left-hand side evaluates to true or
the right-hand side evaluates to true, the result returned is true. Otherwise, the result is false. The
following table shows the possible results.
Left-Hand Side
Right-Hand Side
Result
true
true
true
false
true
true
true
false
true
false
false
false
As with the AND operator, JavaScript likes to avoid doing things that make no difference to the fi nal
result. If the left-hand side is true, then whether the right-hand side is true or false makes no dif-
ference to the fi nal result — it'll still be true. So, to avoid work, if the left-hand side is true, the right-
hand side is not evaluated, and JavaScript simply returns true. The end result is the same — the only
difference is in how JavaScript arrives at the conclusion. However, it does mean you should not rely on
the right-hand side of the OR operator to be executed.
Search WWH ::




Custom Search