Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Another problem with the equality operator has to do with the way a computer deals with floating point numbers. This problem is especially harsh when you try to store integer numbers in the form of floating point numbers (e.g., 3.0). Depending on the underlying machine, the results may differ. You might find out that (5. == 5.00) evaluates to false, and (5.0 == 4.99999999) evaluates to true. Therefore, avoid using equality operators with floating point values. The solution for this problem is presented later in the chapter.

Short-Circuit Logical Operators

Short-circuit logical operators, also called Boolean operators or logical operators, are binary operators that accept Boolean values as their operands. They group multiple relational expressions together. There are three logical operators:

Table 7-10. Logical operators.


Syntax Name Type

|| logical OR binary
&& logical AND binary
! logical NOT unary

Logical OR

operand1 || operand2

The logical OR operator accepts two Boolean values as its operands, and evaluates to a single Boolean value. It evaluates to true if at least one of the operands is true; in other words, the only situation in which it evaluates to false is when both operands are false. Take a look at Table 7-11.

Table 7-11. Logical OR truth table.


Operand1 Operand2 Operand1 || Operand2

true true true
true false true
false true true
false false false

Here are a few examples:

2 > 1 || 3 < 4 // true
1 == 1 || 99 >= 98 // true
"mouse" > "elephant" || 6 < 5 // true
1 == 2 || 5 <= 4 // false

Be careful not to confuse the logical OR operator with the bitwise OR due to their similar meaning and syntax (|| vs. |). The only difference in meaning is that the first handles Boolean operands while the latter handles numeric ones.

The logical OR operator is short-circuit evaluated. As you can see from the truth table, if the first operand is true, it is certain that the expression will evaluate to true. Checking the second operand in such a situation is unnecessary and is avoided by JavaScript.

Logical AND

operand1 && operand2

The logical AND operator is similar to the logical OR operator, except for its truth table:

Table 7-12. Logical AND.


Operand1 Operand2 Operand1 && Operand2

true true true
true false false
false true false
false false false

Both operands must be true for the expression to evaluate to a true value. This operator is similar to the bitwise AND operator, except that the operand types are different, and the logical AND operator operates on the whole operand at once, rather than on a small segment (bit).

Logical AND expressions are also short-circuit evaluated. If the first operand is false, the expression will obviously be false, so the second operand is not evaluated.

Here are some examples:

2 > 1 && 3 < 4 // true
1 == 1 && 99 >= 98 // false
"mouse" > "elephant" && 6 < 5 // false
1 == 2 && 5 <= 4 // false

Logical NOT

!operand1

The logical NOT operator is a simple unary operator which accepts a Boolean value and negates it. This operator is similar to the negation operator, which negates a number (changes its sign). It is even more similar to the bitwise NOT operator (~) which converts all 1 bits to 0 bits, and 0 bits to 1 bits. Although the truth table is obvious, here it is for your reference:

Table 7-13. Logical NOT truth table.


Operand !Operand

true false
false true

Because this is a unary operator, short-circuit evaluation is not relevant.

Here are some examples:

!true // evaluates to false
!(2 > 4) // evaluates to true
!(7 == 7) // evaluates to false
!false // evaluates to true


Caution:  Short-circuit evaluation is mostly useful because it makes your scripts more efficient. However, there are times that this method of evaluation is harmful. For example, you finish writing a complicated script and you want to test it. Depending on certain decisions in the script, short-circuit evaluation is performed, and the interpreter ignores the second operand of various expressions. You might think that the script works perfectly fine, even if there is a data mistype or a syntax error in the second operand. This type of evaluation can mislead you in such situations, but it is unavoidable.


Testing Equality for Floating Point Numbers

We mentioned earlier that the equal operator (==) is not suitable for floating point numbers due to inaccuracy issues. Using logical AND and OR operators you can check if the number is close to the specified value. For example, if you want to evaluate the expression x == 10.0 you use a fuzzy comparison using one of the following expressions, in which [infin] represents a small number (such as 0.001).

(x – 10.0) < [infin] || (10.0 – x) < [infin]

or

x > (10.0 – [infin]) && x < (10.0 + [infin])

More Logical Operators

The conditional operator and the comma operator are logical operators.

Conditional Operator

condition ? trueAlternative : falseAlternative

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us