HTML and CSS Reference
In-Depth Information
where variable is the name assigned to the variable. For example, the following state-
ment creates a variable named emLink :
You can declare multiple variables by entering the variable names in a comma-
separated list. The following statement declares three variables named emLink, user-
Name, and emServer:
JavaScript imposes the following limits on variable names:
• The first character must be either a letter or an underscore character (_).
• The remaining characters can be letters, numbers, or underscore characters.
• Variable names cannot contain spaces.
• You cannot use words that JavaScript has reserved for other purposes; for example, you
cannot name a variable document.
Like other aspects of the JavaScript language, variable names are case sensitive. The
variable names emLink and emlink represent two different variables. One common
programming mistake is to forget this important fact and to use uppercase and lowercase
letters interchangeably in variable names.
To avoid programming
errors, use a consistent
pattern for case in vari-
able names.
Assigning a Value to a Variable
Once a variable has been created or declared, you can assign it a value. The statement to
assign a value to a variable is
variable ƒ=ƒ value ;
where variable is the variable name and value is the value assigned to the variable. For
example, the following statement stores the text string cadler in the userName variable:
You can combine the variable declaration and the assignment of a value in a single
statement. The following statements declare the userName and emServer variables, and
set their initial values:
Note that declaring a variable with the var statement is not required in JavaScript. The
first time you use a variable, JavaScript creates the variable in computer memory. The fol-
lowing statement both creates the director variable (if it has not already been declared in
a previous statement) and assigns it an initial value:
To make your code easier
to interpret, place all of
your variable declara-
tions at the beginning of
your program.
Although it's not required, it's considered good programming style to use a var state-
ment to declare all of your variables. Doing so helps you keep track of the variables a
program uses and also makes it easier for others to read and interpret your code.
Search WWH ::

Custom Search