• Aucun résultat trouvé

Logical operators (Table 2-4) are used to combine two logical expressions into one expression. A logical expression is an expression that evaluates to either true or

Figure 2-7 Bob and Smith are two strings joined together using the addition operator.

ch02.indd 30

ch02.indd 30 5/12/2005 10:46:25 AM5/12/2005 10:46:25 AM

false. The concept of a logical expression might be new to you if you haven’t learned a programming language.

Logical expressions are used in JavaScript to make decisions. You’ll see how this is done in the next chapter, but for now, suppose your JavaScript validates a user ID and password. The fi rst expression that must be evaluated is

userID is equivalent to ScubaBob

ScubaBob is the valid user ID and userID is the user ID entered into the JavaScript.

This is a logical expression because the userID is equivalent to ScubaBob or the userID isn’t equivalent to ScubaBob. That is, this expression is either true or false based on the value of userID.

Here’s how we’d write this logical expression in JavaScript:

userID = = 'ScubaBob'

In this example, userID is a variable whose value is the user ID entered into the JavaScript. The double equal sign (==) is called the equivalency operator, which you learn about in the “Comparison Operators” section of this chapter. The equivalency operator determines whether the operand (that is, the value) on the left side of the operator is the same as the operand on the right side of the operator. The right side of the operator is the string 'ScubaBob', which in this example is the valid user ID.

Now that you have an understanding of a logical expression, let’s see how a logical operator is used to join two logical expressions into one logical expression.

Typically, a JavaScript that validates a user ID also validates a password that is as-sociated with the user ID. Here’s the logical expression that you use to do this:

password == 'diving'

You probably understand this example because this expression is very similar to the previous logical expression. This expression uses the equivalent operator to com-pare the value of the variable password to the valid password diving. If they are the same, this logical expression is true; otherwise, the logical expression is false.

Typically, a JavaScript evaluates both the user ID and the password at the same time and then displays a message on the screen stating whether or not the user’s

Operator Description

&& AND

|| OR

! NOT

Table 2-4 Logical Operators

ch02.indd 31

ch02.indd 31 5/12/2005 10:46:26 AM5/12/2005 10:46:26 AM

logon is valid. Both the user ID and password must be valid for the user’s logon to be valid.

The most effi cient way to validate the user’s logon is to combine the logical expres-sion that validates the user ID with the logical expresexpres-sion that validates the password.

You do this by using the AND logical operator (&&), as shown in the next example:

userID = = 'ScubaBob' && password = = 'diving'

There are three logical expressions in this example. One logical expression vali-dates the user ID and the other logical expression valivali-dates the password, both of which you’ve seen before in this section. The third logical expression is the combi-nation of both logical expressions.

Confused? Let’s walk through the process of how the browser evaluates this ex-ample. Logical expressions are evaluated left to right. First, the browser evaluates the user ID logical expression. If the value of the userID variable is ScubaBob, then the expression is true and looks like this:

True && password == 'diving'

Next, the browser evaluates the user ID logical expression. If the value of the password variable is diving, then this expression is true and looks like this:

TRUE && TRUE

Last, the browser evaluates the remaining logical expression by asking these ques-tions: Is the value on the right side of the AND operator true? Is the value on the left side of the AND operator true? If both answers are true, then the third logical expression is true. However, if either of these is false, then the third logical expression is false.

When using the AND logical operator, both logical expressions on either side of the AND logical operator must be true for the combined logical expression to be true; otherwise, the combined logical expression is false.

Figure 2-8 shows a JavaScript that prompts the user to enter a user ID, and Figure 2-9 shows a JavaScript that prompts the user to enter a password. The values entered by the user are compared to ScubaBob and diving, the valid user ID and password.

You’ll notice something new in this JavaScript. This is an if…else statement. The if…else statement tells the browser to do something if the expression is true;

other-Figure 2-8 The user is asked to enter a user ID.

ch02.indd 32

ch02.indd 32 5/12/2005 10:46:26 AM5/12/2005 10:46:26 AM

wise, if the expression is false, the browser is to do something else. You’ll learn about the if…else statement in the next chapter. For now, you simply need to know that the browser displays the “Logon valid” message on the screen if the expression is true (Figure 2-10). That is, the user entered ScubaBob and diving as the user ID and password. The browser displays “Logon invalid” if the user didn’t enter valid expressions (Figure 2-11).

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Validate userID and Password</title>

</head>

<body>

<script language="Javascript" type="text/javascript">

var userID var password

userID = prompt('Enter user ID',' ') password = prompt('Enter password',' ')

if (userID == 'ScubaBob' && password == 'diving') {

alert('Logon valid') }

else {

alert('Logon invalid') }

-->

</script>

</body>

</html>

Figure 2-9 The user is asked to enter a password.

Figure 2-10 The browser tells the user if the user ID and password are valid.

ch02.indd 33

ch02.indd 33 5/12/2005 10:46:26 AM5/12/2005 10:46:26 AM

The OR logical operator (||) also joins together two logical expressions. How-ever, the combined logical expression is true if either the logical expression on the right side of the OR logical operator is true or the logical expression on the left side of the OR logical operator is true.

Let’s see how this works. Suppose only two people can use your JavaScript. These are Mary and Sue. Your JavaScript prompts the user to enter her fi rst name, which is then assigned to the name variable. The following combined logical expression then determines if the fi rst name is Mary or Sue by using the OR logical operator:

name == 'Mary' || name == 'Sue'

Here’s how the browser evaluates these logical expressions. Assume for this ex-ample that the person entered Sue as the name. First, the browser evaluates the logical expression on the left side of the OR operator. The result is false:

name == FALSE || 'Mary'

Next, the browser evaluates the logical expression on the right side of the OR operator. The result is true:

FALSE || TRUE

Last, the browser evaluates the combined logical expression. If either individual logical expression is true, then the combined logical expression is true. The combined logical expression is false only if both individual logical expressions are false.

The last logical operator in Table 2-4 is the NOT operator (!). The NOT operator is different from the other logical operator in that it does not combine logical ex-pressions. Instead, the NOT operator reverses the logic of a logical expression.

You might have heard a friend say, “I got a big fat raise—not!” The not at the end of this sentence reverses the logic of the fi rst part of the sentence. The fi rst part says,

“I got a big fat raise,” which is a positive statement. The not reverses the positive statement to a negative statement.

Figure 2-11 The browser tells the user if the user ID and password are invalid.

ch02.indd 34

ch02.indd 34 5/12/2005 10:46:27 AM5/12/2005 10:46:27 AM

This is basically how the NOT operator works. Let’s say that you declare a Bool-ean variable in a JavaScript whose value indicates whether the light in the room is turned off or on. Remember that a Boolean variable has either a true or false value.

If the light is off, then the value assigned to the variable is false; a true value is as-signed to the variable if the light is on.

The following example shows how to indicate that the room light is on by using the NOT operator. The fi rst line declares a variable and initializes it to false, indicat-ing that the room light is off. The next line uses the NOT operator to reverse the logical value of the Boolean variable. This says “the room light is not off.” Granted, this is a convoluted way of indicating that the room light is on, but, as you’ll see in the next chapter when you learn how to have your JavaScript make decisions, some-times this is the only way to do it.

Var roomLight = false

!roomLight