How to declare variables and assign values to them
A variable stores a value that can change as the program executes. When you code a JavaScript application, you frequently declare variables and assign values to them. Figure 2-9 shows how to do both of these tasks.
To declare a variable in JavaScript, code the var (for variable) keyword followed by the identifier (or name) that you want to use for the variable. To declare more than one variable in a single statement, you code var followed by the variable names separated by commas. This is illustrated by the first group of examples in this figure.
To assign a value to a variable, you code an assignment statement. This type of statement consists of a variable name, an assignment operator, and an expression. Here, the expression can be a numeric literal like 74.95, a variable name like subtotal, an arithmetic expression, a string literal like "Harris", or a string expression.
The first assignment operator in the table in this figure assigns the value of the expression on the right of the equals sign to the variable on the left. The other five operators are called compound assignment operators. They modify the variable on the left by the value of the expression on the right. When you use these operators, the variable must already exist and have a value assigned to it.
To declare a variable and assign a numeric value to it, you code the keyword var followed by the assignment statement. This is illustrated by the second group of examples in this figure. The first one declares a variable named subtotal and assigns the literal value 74.95 to it. The second one declares a variable named salesTax and assigns the value of the subtotal variable to it after it has been multipled by .1. The third statement assigns the value false to a Boolean variable named isValid. And the fourth statement shows that you can declare and assign values to two or more variables with a single statement. Note here, though, that the var keyword is only coded once.
The last two statements in this group show how you can declare and assign values to string variables. Here, the fifth statement declares and assigns values to two variables named firstName and lastName. Then, the sixth statement concatenates lastName, a literal that consists of a comma and a space, and firstName, and places the result in a new variable named fullName.
The next group of examples illustrates the use of compound assignment statements. Here, the first statement assigns a value of 24.50 to a variable named subtotal, and the second statement uses the += operator to add a value of 75.50 to the value already in subtotal. This means that subtotal contains 100.00. Then, the third statement uses the *= operator to multiply the value in subtotal by .9. As a result, subtotal has a value of 90.
The rest of the statements in this group work with string variables and string expressions. If you study them, you can see that they're similar to numeric assignment statements. However, the += operator is the only compound assignment operator that you can use with strings. The last two statements in this group show that if an expression contains both string and numeric values, the numeric values are converted to strings before the expression is evaluated.
How to declare variables without assigning values to them
var subtotal; // declares a variable named subtotalvar lastName, state, zipCode; // declares three variables
The assignment operators
var subtotal = 74.95; // subtotal is 74.95var salesTax = subtotal * .1; // salesTax is 7.495var isValid = false; // Boolean value is falsevar zipCode = "93711", state = "CA"; // one statement with two assignmentsvar firstName = "Ray", lastName = "Harris"; // assigns two string valuesvar fullName = lastName + ", " + firstName; // fullName is "Harris, Ray"
How to code compound assignment statements
var subtotal = 24.50;subtotal += 75.50; // subtotal is 100subtotal *= .9; // subtotal is 90 (100 * .9)var firstName = "Ray", lastName = "Harris";var fullName = lastName; // fullName is "Harris"fullName += ", "; // fullName is "Harris, "fullName += firstName; // fullName is "Harris, Ray"var months = 120, message = "Months: ";message += months; // message is "Months: 120"
Description
Figure 2-9 How to declare variables and assign values to them