Logical Operators


Like the comparison operators we just took a look at, you use logical operators when creating test conditions that evaluate to a value of true or false. Using logical operators, you can create more powerful test conditions than just using comparison operators alone.

Suppose, for example, that you want to make sure the value in a variable named temperature is between 65 and 75. You can use the greater-than comparison operator, > , to make sure temperature is greater than 65, and the less-than comparison operator, < , to make sure that temperature is less than 75. But how can you make sure both conditions are true in the same if statement?

That's where the logical operators come in, because they enable you to tie test conditions together. For example, the && logical operator is called the logical AND operator. If you use it like this: expression1 && expression2 , both expression1 and expression2 must be true for the whole expression expression1 && expression2 to be true; otherwise the whole expression's value is false. Here's how that looks in an example, where I'm checking the temperature:

(Listing 02-09.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Working With Logical Operators           </TITLE>      </HEAD>      <BODY>          <H1>Working With Logical Operators</H1>          <SCRIPT LANGUAGE="JavaScript">          <!--  var temperature = 68   if(temperature > 65 && temperature < 75) {   document.write("In the comfort zone!")   }  // -->          </SCRIPT>       </BODY>  </HTML> 

Figure 2.7 shows the results of this code (which shows inside the comfort zone, temperature-wise).

Figure 2.7. Using the logical operators.

graphics/02fig07.gif

Besides the && logical AND operator, you can also use the logical OR operator. The OR operator returns true if its first operand or its second operand is true (unlike AND, where you need both operands to be true to get a value of true), and false otherwise. In the following example, I use the <= less-than-or-equal-to and >= greater-than-or-equal-to operators to check whether we're outside a temperature comfort zone:

(Listing 02-10.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Working With Logical Operators          </TITLE>      </HEAD>      <BODY>          <H1>Working With Logical Operators</H1>          <SCRIPT LANGUAGE="JavaScript">          <!--  var temperature = 62   if(temperature <= 65  temperature >= 75) {   document.write("Outside the comfort zone!")   }  // -->          </SCRIPT>       </BODY>  </HTML> 

There's also another logical operator: the logical NOT operator, ! . You use this operator to reverse the logical sense of an expression. For example if expression1 evaluates as true, !expression1 will return false; and if expression1 evaluates as false, !expression1 will return true.

Tip

The && and operators are also called short-circuit operators in JavaScript. In particular, because false && anyExpression is always false, and true anyExpression is always true, so in both cases, JavaScript does not even evaluate anyExpression . That means if evaluating anyExpression has side effects, such as calls to functions, those callsand so their side effectswill not occur, which is why these operators are called short-circuit operators.




Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net