home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Table 18.2 Comparison Operators that Allow Two JavaScript Operands to Be Compared in a Variety of Ways

Operator Read It As Returns TRUE When

== equals The two operands are equal.
!= does not equal The two operands are unequal.
< less than The left operand is less than the right operand.
<= less than or equal to The left operand is less than or equal to the right operand.
> greater than The left operand is greater than the right operand.
>= greater than or equal to The left operand is greater than or equal to the right operand.

You might find it helpful to think of the comparison operators as questions. When you write the following:

(x >= 10)

you’re really saying, “Is the value of variable x greater than or equal to 10?” The return value answers the question, TRUE or FALSE.


TROUBLESHOOTING
You may be asking yourself, “Why do my tests for equality always succeed, even when I know that the two quantities are sometimes different?”
A common mistake in JavaScript, as in C, C++, or Java, is mixing up the = operator—used to set one quantity equal to another—and the == operator—used to test two quantities for equality. The following code tests to see whether the variable a is equal to 10. If it is, this code writes out the following line:
if (a == 10)
document.writeln(“a is equal to 10!”)
On the other hand, the following code sets a equal to 10 and returns TRUE, and thus always writes out the following line:
if (a = 10)
document.writeln(“a is equal to 10!”)

Logical Operators Comparison operators compare quantity or content for numeric and string expressions. Sometimes, however, you need to test a logical value such as whether a comparison operator returns TRUE or FALSE. JavaScript’s logical operators enable you to compare expressions that return logical values. The following are JavaScript’s logical operators:

  &&, read as “and.” The && operator returns TRUE if both its input expressions are true. If the first operand evaluates to false, && returns FALSE immediately, without evaluating the second operand. Here’s an example:
x = TRUE && TRUE;      // x is TRUE
x = FALSE && FALSE;    // x is FALSE
x = FALSE && TRUE;     // x is FALSE
  ||, read as “or.” This operator returns TRUE if either of its operands is true. If the first operand is true, || returns TRUE without evaluating the second operand. Here’s an example:
x = TRUE || TRUE;      // x is TRUE
x = FALSE || TRUE;     // x is TRUE
x = FALSE || FALSE;    // x is FALSE
  !, read as “not.” This operator takes only one expression and returns the opposite of that expression; !TRUE returns FALSE, for example, and !FALSE returns TRUE.

Note that the “and” and “or” operators don’t evaluate the second operand if the first operand provides enough information for the operator to return a value. This process, called short-circuit evaluation, can be significant when the second operand is a function call. For example:

keepGoing = (userCancelled == FALSE) && (theForm.Submit())

If userCancelled is TRUE, the second operand, which submits the active form, is not called.

String Operators You can use a few of the operators previously listed for string manipulation as well. All the comparison operators can be used on strings, too; the results depend on standard lexicographic ordering (ordering by the ASCII values of the string characters), but comparisons aren’t case sensitive. Additionally, you can use the + operator to concatenate strings, returning a string made up of the original strings joined together. The expression

str = “Hello, “ + “World!”;

would assign the resulting string “Hello, World!” to the variable str.

Controlling Your JavaScripts

Some scripts you write will be simple. They will execute the same way every time, one time per page. If you add a JavaScript to play a sound when users visit your home page, for example, it doesn’t need to evaluate any conditions or do anything more than one time. More sophisticated scripts might require that you take different actions under different circumstances. You might also want to repeat the execution of a block of code—perhaps by a set number of times or as long as some condition is TRUE. JavaScript provides constructs for controlling the execution flow of your script based on conditions, as well as for repeating a sequence of operations.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.