• Aucun résultat trouvé

Web pages contain a lot of information along with a few pictures sprinkled about to catch your attention. In HTML, you place information you want to display between varieties of HTML tags. You place “Hello, world!” between the open <h1> and close </h1> tags, which cause that message to be displayed on the web page. In-formation that you place in the code of a web page or JavaScript is called a value.

For example, the “Hello, world!” script that you wrote in the JavaScript in Chapter 1 is a value. A variable is basically a placeholder that holds a spot for data that can be changed during the execution of a program.

Values

In HTML, all values are treated as text. That is, when you enter 10, HTML treats it not as a number that can be used in a calculation, but as a number that you might use in a street address, such as 10 Downing Street. JavaScript uses six kinds of val-ues: number, string, Boolean, null, object, and function.

Number

A number is a numeric value that can be used in a calculation.

String

A string is text that is enclosed within quotation marks. It is called a string because characters are strung together to form the text. A string can also contain numbers, but those numbers can’t be used in a calculation unless the developer performs some JavaScript magic to it, which you’ll learn about later in this book. So the number in 10 Downing Street is part of a string and cannot be directly used in a calculation.

Boolean

A Boolean is a value that is either false or true, which is represented as zero and/or non-zero. As you might surmise, a Boolean value is used to help a JavaScript make a decision, such as evaluating whether or not the user entered her e-mail address in an order form.

ch02.indd 16

ch02.indd 16 5/12/2005 10:46:22 AM5/12/2005 10:46:22 AM

Null

There is nothing to the null value. Really—I mean nothing. That’s what null means.

Null is the absence of any value. You might wonder why you’d need to use such a value, but as you’ll see when you start writing sophisticated JavaScripts, there will be times when you need to use a variable (a placeholder for a value) to represent no value (null) until your JavaScript assigns a value to the variable. For example, you probably want to assign null to the variable used for a customer’s fi rst name until the customer enters his or her name on the form.

Objects

You learned about objects in Chapter 1. An object is a value. This means that a document is a value, and so are a window and a form. You’ll become very familiar with objects when you start using them in your JavaScript a bit later.

Functions

A function performs an action when you call the function in a JavaScript—such as when you called the alert() function to display a message on the screen in Chapter 1. Two kinds of functions are used in JavaScript: predefi ned functions and custom functions. A predefi ned function is already created for you in JavaScript, such as the alert() function. A custom function is a function that you create.

You’ll learn all about functions in Chapter 5, but let’s take a peek at what you’ll be learning.

Following is a custom function defi nition that displays “Hello, world!” on the screen. A function defi nition is part of a JavaScript that the browser executes when-ever the function is called somewhere else in the JavaScript. This example of a function defi nition contains one statement that you’ll remember from Chapter 1. In this example, the name of this function is DisplayHelloWorld(). This tells the browser to execute the statement found in the defi nition of the DisplayHel-loWorld() function.

function DisplayHelloWorld() {

alert('Hello, world!') }

Variables

Literal values are fi ne to use if you already know the value when you write your Ja-vaScript. However, sometimes the value isn’t known until your JavaScript is running.

ch02.indd 17

ch02.indd 17 5/12/2005 10:46:22 AM5/12/2005 10:46:22 AM

Let’s say that your JavaScript calculates the sales tax on the purchase price of an item.

You probably know the percentage value of the sales tax when you write the Java- Script, so you can write the literal value of the percentage into your JavaScript. You don’t know the purchase price of the item until the customer selects the item while your JavaScript runs. This poses a dilemma. How can you write the sales tax calcula-tion into your JavaScript without knowing the purchase price of the item?

The solution is to use a variable in place of the purchase price. You can think of a variable as an empty cardboard box. You place a label on the box on which you write a name. You place a value inside the box. Each time you want to refer to the value, you simply refer to the name of the box.

Let’s return to our sales tax example to see how this works. First, we’ll need a box in which to store the purchase price. Let’s write PurchasePrice on the label of the box (Figure 2-1). We could write any name on the label, but it is less confusing if the name used represents the value stored inside the box.

Next, we’ll write the math expression to calculate the sales tax (the Purchase-Price times the sales tax percentage of 6 percent):

PurchasePrice * .06

Notice that the name on the label of the box (PurchasePrice, the variable) is used to refer to the purchase price in this calculation. We could have used the actual pur-chase price, but we don’t know the purpur-chase price until the user enters the purpur-chase price into our application. Until then, all we can do is refer to the variable where the browser will store the purchase price after it is entered into the application.

When the browser sees PurchasePrice in the JavaScript, the browser knows that PurchasePrice is a label for a variable that contains the value of the pur-chase price. The browser then copies the value entered by the user, replaces the PurchasePrice variable with the value, and performs the calculation.

Figure 2-1 A variable is similar to a cardboard box that contains a value. You refer to the label on the box whenever you want to use the value inside the box.

ch02.indd 18

ch02.indd 18 5/12/2005 10:46:22 AM5/12/2005 10:46:22 AM