HTML and CSS Reference
In-Depth Information
Table 9-7 Naming Conventions for JavaScript Variables
Valid Name
Invalid Name
Name must begin with a letter or underscore
Rest of name must be letters, numerals, or underscores
Name may not use spaces, hyphens, or punctuation
zip.code or zip code
Name may not contain JavaScript objects, properties,
and reserved words
JavaScript variables are considered global, unless the variable is defined within a
user-defined function, in which case it is considered a local variable. Global means that
the variable value is available for use anywhere inside the HTML file Web page. To define
a variable as global, it must be declared in the <script> section before any of the user-
defined functions. Local means that the variable's value is available only in the function
in which it is defined.
A variable's data type , the type of data it stores, such as text or numbers, must be
known so the computer knows how to store and manipulate the data. JavaScript has four
data types: numeric, string, date, or Boolean. Numeric data types hold numbers. String
data types are variables that hold characters or a combination of letters, numbers, or symbols.
Date data types contain a date and time. Boolean data types contain logical data that
can be one of two values, such as True/False or Yes/No.
JavaScript variables are loosely typed , which means they do not have to be assigned
an initial specific data type as in other programming languages. Instead, JavaScript defines
the data type by declaring the variable with an initial value. This feature allows variables to
be flexible and store any data type. Web developers, however, do not recommend chang-
ing a variable's data type in the middle of JavaScript code. This action may create an error,
which can be very difficult to find. Table 9-8 shows the general form of declaring a vari-
able and assigning a value to it.
Undefined Variables
If a variable's value, which
has not been defined or
declared previously, is used
or displayed on the Web
page, JavaScript assigns
the value “undefined”
to that variable. An
undefined variable
can cause errors in
mathematical calculations.
Table 9-8 Assigning Values to Variables
General form:
var variableName=value
where var is an optional keyword to designate a variable; variableName is a valid variable name;
and value is the string, numeric, date, or Boolean value being assigned to the variable
var FilmStyle=”Drama” // This variable is a string data type
var length=6.5 // This variable is a numeric data type
var finished=true // This variable is a Boolean data type
In the examples in Table 9-8, the keyword var, meaning variable, appears before the
variable name. A keyword , or reserved word , is a word with special meaning in a pro-
gramming language. The JavaScript var keyword is optional for global variables; however,
it is good programming practice to precede the variable name with the var keyword the
first time it is defined. In addition, the var keyword is required for local variables defined
within a function.
Search WWH ::

Custom Search