HTML and CSS Reference
In-Depth Information
And if you were assigning a floating-point number:
float x = 44.5;
Languages that require that you specify a data type are called “strongly typed” lan-
guages. JavaScript, conversely, is a dynamically or loosely typed language, meaning that
you do not have to specify the data type of a variable. In fact, doing so will produce an
error. With JavaScript, you would simply say:
n=5;
x = 44.5;
and JavaScript will figure out what type of data is being stored in n and x .
3.2.1 Valid Names
Variable names consist of any number of letters (an underscore counts as a letter) and
digits. The first character must be a letter or an underscore. Because JavaScript keywords
do not contain underscores, using an underscore in a variable name can ensure that you
are not inadvertently using a reserved keyword. Variable names are case sensitive; for
example, Name, name , and NAme are all different variable names. Refer to Table 3.2.
Table 3.2 Valid and Invalid Variable Names
Valid Variable Names
Invalid Variable Names
name1
10names
price_tag
box.front
_abc
name#last
Abc_22
A-23
A23
5
3.2.2 Declaring and Initializing Variables
Variables must be declared before they can be used. To make sure that variables are
declared first, you can declare them in the head of the HTML document. There are two
ways to declare a variable: with or without the keyword var . Although laziness might get
the best of you, it is a better practice to always use the var keyword.
You can assign a value to the variable (or initialize a variable) when you declare it,
but it is not mandatory, unless you omit the var keyword. If a variable is declared but
not initialized, it is “undefined.”
 
 
 
Search WWH ::




Custom Search