HTML and CSS Reference
In-Depth Information
EXAMPLE
5.11 (
CONTINUED
)
2
document.write("num1 && num2 is "+(
num1 && num2
)+".<br />");
3
document.write("num1 || $num2 is "+(
num1 || num2
)+".<br />");
4
document.write("! num1 is " +
!num1
+".<br />");
5
document.write("!(num1 && num2) is "+
!(num1 && num2)
+".<br />");
6
document.write("!(num1 && num3) is "+
!(num1 && num3)
+".<br />");
</script>
</h3>
</body>
</html>
EXPLANATION
1
Three variables,
num1
,
num2
, and
num3,
are initialized.
2
The
&&
operator expects both of its operands to be true, if the expression is to be
true. A true value is any number that is not zero. In the expression,
50 && 100
,
both operands are true. The value of the last true operand,
100
, is returned.
3
The
||
operator expects only one of its operands to be true if the whole expression
is to be true.
50 || 100
is true because the first operand evaluates to a nonzero val-
ue. Because
50
is true and only one operand must be true, the evaluation stops
here and
50
is returned.
4
The
!
(NOT) operator negates its operand.
! 50
means
! true
; that is,
false
.
5
Because the expression
num1 && num2
is enclosed in parentheses, it is evaluated
first, resulting in
50 && 100
,
true
. Then the
!
(NOT) operator evaluates
!
(true),
resulting in Boolean
false
.
6
The expression,
num1 && num3
, enclosed in parentheses, is evaluated first. Be-
cause
num3
is
0
, the expression evaluates to false.
!
(false) is
true
. See Figure 5.16.
Figure 5.16
Output from Example 5.11.