• No results found

Assigning a Value to a Variable

Typically, the value of a variable changes while your JavaScript runs, and the initial value of the variable is replaced with another value. This is the case when using the

PurchasePrice variable in our example.

We used 100 as the initial value of the PurchasePrice variable to simulate receiving the purchase price from a customer who uses your JavaScript. If this were a real JavaScript application, we’d use 0 as the initial value since the customer hasn’t as yet purchased an item. Then we’d replace the 0 value with the purchase price of the item selected by the customer (as shown in the JavaScript a few para- graphs later).

JavaScript has an easy way for you to ask the user of your application to enter in- formation into your JavaScript—by calling the prompt() function. The prompt()

function displays text within a prompt dialog box (see Figure 2-2) and then waits for the user to enter information and click the OK button. The information entered by the user is then returned to your JavaScript so your script can process it.

You can use the prompt() function to have the user enter the purchase price, and then we’ll assign the purchase price to the PurchasePrice variable before calculat- ing and displaying the sales tax. Here is the syntax for calling a prompt() function:

prompt('message', 'default <F102>value')

Figure 2-2 The prompt() function displays a prompt dialog box, where the user enters the purchase price.

ch02.indd 21

Notice that the prompt() function is called similarly to how you called the

alert() function to display the alert dialog box in Chapter 1. You need to provide two pieces of information to the prompt() function: The fi rst is the message; this is the text that tells the user what they should enter into the prompt dialog box. The second piece is the default value, which is the value given to your JavaScript by the

prompt() function if the user doesn’t enter a value into the prompt dialog box. In some cases when a default value isn’t used, you can simply add empty quotation marks, as shown in the following example:

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Transitional//EN"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>

<title>Receiving a value from the user</title> </head>

<body>

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

var PurchasePrice = 0 PurchasePrice=

prompt('Please enter the purchase price.', ' ') var SalesTax = PurchasePrice * .06

alert('Sales tax is $' + SalesTax) -->

</script> </body> </html>

After the comment characters (<!--), the fi rst line declares the Purchase- Price variable and initializes it to 0, because we don’t know the value of the purchase price when we’re writing the JavaScript.

The next line calls the prompt() function in an assignment statement that asks the user to enter the purchase price. An assignment statement tells the browser to replace the current value of a variable with a new value. There are three parts to an assignment statement: the name of the variable (PurchasePrice), the assign- ment operator (=), and the new value, which is the value entered by the person and returned by the prompt() function. Now the value of the PurchasePrice

variable is the value entered by the user.

The next line declares the SalesTax variable and initializes it with the sales tax calculated by multiplying the value entered by the user by .06, which is the decimal value of the sales tax.

TIP

ch02.indd 22

The next line displays the sales tax on the screen by calling the alert() func- tion. As you recall from Chapter 1, the alert() function requires you to place the message that you want displayed between the parentheses. In this example, we use the plus operator (+) to place the value of the SalesTax variable at the end of the text statement “Sales tax is $” (see Figure 2-3).

Strings

Although our examples use numbers to show you how to initialize and assign val- ues to a variable, you can also initialize and assign words and punctuation to a variable. To do this, you write the declaration statement and assignment statement the same way shown in previous examples, except you enclose words and punctua- tion within quotation marks.

TIP

TIP JavaScript allows both single and double quotation marks to be used to

designate a string. It is always better to use single quotation marks, though, because the double quotation marks might interfere with double quotation marks used in the HTML page. Using single quotation marks avoids any potential interference.

Here is a new example. The fi rst line declares a variable called ProductName

and initializes it with the text Soda. The second line assigns Water to the Pro- ductName variable:

var ProductName = 'Soda' ProductName = 'Water'

Figure 2-3 The alert() function displays the alert dialog box that displays the sales tax that is calculated by the JavaScript.

ch02.indd 23