• Aucun résultat trouvé

The conditional operator (also known as the ternary operator) (Table 2-7) is differ-ent from the other operators that you’ve learned about in this chapter. The conditional operator tells the browser to take a specifi c action after evaluating an expression.

The conditional operator has three parts: The fi rst part is a logical expression, which you’ll recall is an expression that evaluates to either true or false. The second part is the action the browser must take if the expression is true. The third part is the action the browser must take if the expression is false. The fi rst and second parts of the conditional operator are separated by a question mark (?). The second part and the third parts are separated by a colon (:).

The best way to gain an understanding of the conditional operator is to see it put into action. The following example revisits the validation process for user ID and password. However, this time the browser is told to take specifi c action if the user ID and password are valid or invalid.

userID == 'ScubaBob' && password ==

'diving' ? message = 'Approved' : message = 'Rejected' The fi rst thing to do whenever you see the conditional operator is to identify all three parts. The fi rst part in this example appears to the left of the question mark.

This is the same expression that you saw earlier in this chapter. The second part of the conditional operator is to the right of the question mark, which assigns the word Approved to the variable message. The third part of the conditional operator appears to the right of the colon and assigns the word Rejected to the variable message.

If the value of the userID variable is ScubaBob and diving is the value of the password variable, then the expression part of the conditional operator is true.

The browser is told to execute the second part of the conditional operator, which assigns the word Approved to the message variable. The third part of the condi-tional operator is not executed.

If the value of the userID variable is not ScubaBob and/or diving is not the value of the password variable, then the expression part of the conditional opera-tor is false. The browser is told to execute the third part of the conditional operaopera-tor, which assigns the word Rejected to the message variable. The second part of the conditional operator is not executed.

Operator Description

Expression ? value1 : value2 If expression is true, then use value1;

otherwise, use value2 Table 2-7 Conditional Operator

ch02.indd 39

ch02.indd 39 5/12/2005 10:46:28 AM5/12/2005 10:46:28 AM

Looking Ahead

In this chapter you learned how to store literal values such as a number or words temporarily in computer memory by declaring and initializing a variable. A vari-able is like a cardboard box. You create the box (declare a varivari-able), place a label on the box (name a variable), and place a value into the box (initialize a variable or assign a value to a variable).

Variables and literal values are used with operators to construct an expression.

An operator is a symbol that tells the browser how to evaluate the expression. An operator tells the browser to perform an operation on values or variables on one or both sides of the operator. These values or variables are called operands.

Arithmetic operators are used to tell the browser to perform arithmetic. Logical operators are used to combine two expressions. The assignment operator is used to copy a value on the left side of the operator to the right side of the operator, which is usually a variable. Comparison operators compare two values. The conditional operator tells the browser to evaluate a condition and to do something if the condi-tion is true and something else if the condicondi-tion is false.

Expressions can become complex, especially when several operations are per-formed in the same expression. The browser follows a set of rules called the order of operations when evaluating an expression. These rules tell the browser how to evaluate a complex expression. You can simplify a complex expression by placing parentheses around portions of the expression that you want executed fi rst by the browser.

Variables, operators, and expressions are the nitty-gritty of JavaScript. Think of them as the brick and mortar of building a JavaScript application. In the next chapter, you’ll use variables, operators, and expressions to tell the browser how to make a decision and how to execute JavaScript statements repeatedly within a JavaScript.

Quiz

1. You reference computer memory by using a. Operator

b. Variable name c. Literal value d. Variable type

ch02.indd 40

ch02.indd 40 5/12/2005 10:46:28 AM5/12/2005 10:46:28 AM

2. What tells the browser to do something?

a. Mathematical expression b. JavaScript expression c. JavaScript statement d. Logical expression

3. In the expression 1 + 1, what part of the expression are the numbers?

a. Operand b. Operator c. Modulus d. Incrementer

4. In the expression 1 + 1, what part of the expression is the plus sign?

a. Operand b. Operator c. Modulus d. Incrementer

5. What is happening in the expression ++a?

a. The value of a is increased by 2.

b. The value of a is increased by 1.

c. The value of a is multiplied by itself.

d. Nothing; this is not a valid JavaScript expression.

6. Evaluate this expression: 7 < 10 ? 'You win.' : 'You lose.' a. 10

b. You lose.

c. You win.

d. 7

7. What does the && operator do?

a. Evaluates true if expression on its left and right are both true b. Evaluates true if expression on its left or right is true

c. Evaluates true if neither expression on its left or right is true d. Combines the expression on its right with the expression on its left

ch02.indd 41

ch02.indd 41 5/12/2005 10:46:29 AM5/12/2005 10:46:29 AM

8. True or False: The ++ can be on either the right (c = a++) or left (c = ++a) side of an expression without having any effect on the expression.

a. True b. False

9. True or False. The x += y expression adds values of x and y and stores the sum in x.

a. True b. False

10. True or False. The != operator makes a false true.

a. True b. False

ch02.indd 42

ch02.indd 42 5/12/2005 10:46:29 AM5/12/2005 10:46:29 AM

43

CHAPTER

Condition