HTML and CSS Reference
EXAMPLE 5.11 ( CONTINUED )
document.write("num1 && num2 is "+( num1 && num2 )+".<br />");
document.write("num1 || $num2 is "+( num1 || num2 )+".<br />");
document.write("! num1 is " + !num1 +".<br />");
document.write("!(num1 && num2) is "+ !(num1 && num2) +".<br />");
document.write("!(num1 && num3) is "+ !(num1 && num3) +".<br />");
Three variables, num1 , num2 , and num3, are initialized.
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.
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.
The ! (NOT) operator negates its operand. ! 50 means ! true ; that is, false .
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 .
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.