FREE BOOK

Chapter 2: How to code a JavaScript application

Posted by Murach Free Book | Internet & Web November 02, 2009
This chapter presents a subset of JavaScript and DOM scripting that will soon have you writing significant applications. If you don't have any programming experience, this chapter also makes a great aptitude test. If you read it and can do the exercises at the end of the chapter, you're ready for the rest of this book.

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 subtotal
var lastName, state, zipCode; // declares three variables

The assignment operators

Operator Description
= Assigns the result of the expression to the variable.
+= Adds the result of the expression to the variable.
-= Subtracts the result of the expression from the variable.
*= Multiplies the variable by the result of the expression.
/= Divides the variable by the result of the expression.
%= Stores the modulus of the variable and the result of the expression in the variable.

How to declare variables and assign values to them

var subtotal = 74.95; // subtotal is 74.95
var salesTax = subtotal * .1; // salesTax is 7.495
var isValid = false; // Boolean value is false
var zipCode = "93711", state = "CA"; // one statement with two assignments
var firstName = "Ray", lastName = "Harris"; // assigns two string values
var fullName = lastName + ", " + firstName; // fullName is "Harris, Ray"

How to code compound assignment statements

var subtotal = 24.50;
subtotal += 75.50; // subtotal is 100
subtotal *= .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

  • A variable stores a value that can change as the program executes.
  • To declare a variable, code the keyword var and a variable name. To declare more than one variable in a single statement, code var and the variable names separated by commas.
  • To assign a value to a variable, you use an assignment statement that consists of the variable name, an assignment operator, and an expression. When appropriate, you can
    declare a variable and assign a value to it in a single statement.
  • Within an expression, a string literal is enclosed in quotation marks. A numeric literal is a valid integer or number that isn't enclosed in quotation marks.
  • When the expression in an assignment statement contains both strings and numbers, the numbers are converted to strings before the expression is evaluated.

Figure 2-9 How to declare variables and assign values to them

Total Pages : 20 89101112

comments