Section 5.7. Logical Operators


5.7. Logical Operators

The logical operators typically perform Boolean algebra. They are often used in conjunction with comparison operators to express complex comparisons that involve more than one variable, and are also frequently used with the if, while, and for statements.

5.7.1. Logical AND (&&)

When used with Boolean operands, the && operator performs the Boolean AND operation on the two values: it returns TRue if and only if both its first operand and its second operand are true. If one or both of these operands is false, it returns false.

The actual behavior of this operator is somewhat more complicated. It starts by evaluating its first operand, the expression on its left. If the value of this expression can be converted to false (for example, if the left operand evaluates to null, 0, "", or undefined), the operator returns the value of the left-side expression. Otherwise, it evaluates its second operand, the expression on its right, and returns the value of that expression.[*]

[*] In JavaScript 1.0 and JavaScript 1.1, if the left-side expression evaluates to false, the && operator returns false rather than returning the unconverted value of the left-side expression.

Note that, depending on the value of the left-side expression, this operator may or may not evaluate the right-side expression. You may occasionally see code that purposely exploits this feature of the && operator. For example, the following two lines of JavaScript code have equivalent effects:

 if (a == b) stop( ); (a == b) && stop( ); 

While some programmers (particularly Perl programmers) find this a natural and useful programming idiom, I recommend against using it. The fact that the right side is not guaranteed to be evaluated is a frequent source of bugs. Consider the following code, for example:

 if ((a == null) && (b++ > 10)) stop( ); 

This statement probably does not do what the programmer intended because the increment operator on the right side is not evaluated whenever the comparison on the left side is false. To avoid this problem, do not use expressions with side effects (assignments, increments, decrements, and function calls) on the right side of && unless you are quite sure you know exactly what you are doing.

Despite the fairly confusing way that this operator actually works, it is easiest, and perfectly safe, to think of it as merely a Boolean algebra operator. Although it does not actually return a boolean value, the value it returns can always be converted to a boolean value.

5.7.2. Logical OR (||)

When used with Boolean operands, the || operator performs the Boolean OR operation on the two values: it returns true if either the first operand or the second operand is true, or if both are true. If both operands are false, it returns false.

Although the || operator is most often used simply as a Boolean OR operator, it, like the && operator, has more complex behavior. It starts by evaluating its first operand, the expression on its left. If the value of this expression can be converted to TRue, it returns the unconverted value of the left-side expression. Otherwise, it evaluates its second operand, the expression on its right, and returns the value of that expression.[]

] In JavaScript 1.0 and JavaScript 1.1, if the left-side expression can be converted to true, the operator returns TRue and doesnt return the unconverted value of the left-side expression.

As with the && operator, you should avoid right-side operands that include side effects, unless you purposely want to use the fact that the right-side expression may not be evaluated.

Even when the || operator is used with operands that are not boolean values, it can still be considered a Boolean OR operator because its return value, whatever the type, can be converted to a boolean value.

On the other hand, you may sometimes see code that uses || with nonboolean operands and takes advantage of the fact that its return value is not a boolean. An idiomatic usage of this operator is to select the first value in a set of alternatives that is defined and non-null (that is, the first value that does not convert to false). Here is an example:

 // If max_width is defined, use that.  Otherwise look for a value in // the preferences object.  If that is not defined use a hard-coded constant. var max = max_width || preferences.max_width || 500; 

5.7.3. Logical NOT (!)

The ! operator is a unary operator; it is placed before a single operand. Its purpose is to invert the boolean value of its operand. For example, if the variable a has the value true (or is a value that converts to true), !a has the value false. And if the expression p && q evaluates to false (or to a value that converts to false), !(p && q) evaluates to true.

The ! operator converts its operand to a boolean value (using the rules described in Chapter 3) if necessary before inverting the converted value. This means that you can convert any value x to its equivalent boolean value by applying this operator twice: !!x.




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

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