Java Reference
In-Depth Information
true is that the result of the assignment expression is the value of the right-hand side expression and
this is the number 100, which is then implicitly converted to a Boolean and any number besides 0 and
NaN converts to true.
Logical Operators
You should have a general idea of how to use conditions in if statements now, but how do you use a
condition such as “Is degFahren greater than zero but less than 100?” There are two conditions to test
here. You need to test whether degFahren is greater than zero and whether degFahren is less than 100.
JavaScript enables you to use such multiple conditions. To do this, you need to learn about three more
operators: the logical operators AND, OR, and NOT. The symbols for these are listed in the following table.
Operator
Symbol
AND
&&
OR
||
NOT
!
Notice that the AND and OR operators are two symbols repeated: && and || . If you type just one symbol,
& or | , strange things will happen because these are special operators called bitwise operators used in
binary operations — for logical operations you must always use two.
After you've learned about the three logical operators, you'll take a look at how to use them in if state-
ments, with plenty of practical examples. So if it seems a bit confusing on fi rst read, don't panic. All will
become clear. Let's look at how each of these works, starting with the AND operator.
AND
Recall that we talked about the left-hand side (LHS) and the right-hand side (RHS) of the operator. The
same is true with the AND operator. However, now the LHS and RHS of the condition are Boolean values
(usually the result of a condition).
The AND operator works very much as it does in English. For example, you might say, “If I feel cold and I
have a coat, then I'll put my coat on.” Here, the left-hand side of the “and” word is “Do I feel cold?” and
this can be evaluated as true or false . The right-hand side is “Do I have a coat?” which again is evalu-
ated to either true or false . If the left-hand side is true (I am cold) and the right-hand side is true (I do
have a coat), then you put your coat on.
This is very similar to how the AND operator works in JavaScript. The AND operator actually produces
a result, just as adding two numbers together produces a result. However, the AND operator takes two
Boolean values (on its LHS and RHS) and results in another Boolean value. If the LHS and RHS condi-
tions evaluate to true , the result will be true . In any other circumstance, the result will be false .
Search WWH ::




Custom Search