A variable is an amount of memory space reserved to store a piece of
information of your code. To use such a memory space, you must first let the browser know that you would need it. For the browser to reserve such a
memory space for you, and to use it eventually, you must give it a name. The memory space that is reserved will be used to receive some values from your application and to make such values available when your code needs
them. The content of this reserved memory space can change or vary
regularly. For example, if you create a web page that acts as an employment application and include a text box for the first name, the content of the
first name text box will be different depending on each visitor. The memory space that stores the first name of the visitors will vary from one visitor to another visitor. For this reason, it is called a variable.
The primary piece of information you must specify about a
variable is its name and there are rules you must follow.
The name of the variable:
- Must start with a letter or an underscore character
- After the first character, can contain letters, digits, and underscores
- Cannot contain space
When naming your objects, you should be a little explicit. A name such
as d intended to designate a date hired is difficult to figure out. On the other hand, if you plan to use a variable that represents a customerís address, you can just call the
variable address. If the name is a combination of words, such as
firstname, you start the first letter of the first part in lowercase and
the first character of the other parts in uppercase. For example, instead of
studentname, you can use studentName; instead of dateofbirth, use
use different naming conventions. For example, some programmers use the
underscore to append two names of a variable. You are free to adopt which
ever convention suits you.
To use a variable in your script, you can just type
it. Here is an example:
With such a variable, you can perform any of the operations we will learn
Once the variable has been used somewhere, the browser
reserves a space in memory for it. If you use another or new name for a
variable in the same script, if the new variable has a different name than
a former variable, the browser would reserve a new space in memory
for it. You can continue introducing variables like that as your script
Be careful not to mistakenly type the name of variable that
you want to reuse in your program. For example, if you have a variable
named numberOfStudents in your script, then find out that you want to use
it in an operation, if you type nbrOfStudents instead of numberOfStudents,
since both names are different, a new memory space would be reserved for
nbrOfStudents because the browser would consider that nbrOfStudents is a
new variable. Trying to use both variables as one would lead to
unpredictable results. To avoid this confusion, an alternative is to first tell the browser that you have a
variable you want to use. By doing this, the browser would be a
"witness" that a certain variable exists. This is not a
bulletproof solution but can help to improve your code.
Letting the interpreter know about a variable is referred to as declaring the variable. To declare a variable, use the
var keyword, followed by a name for the variable, followed by a
semi-colon. Here are examples of declaring variables:
Although the var keyword is used to declare a variable, it can be used for any kind of variable.
It can be used to declare a variable that is used for a natural number
(integer), a decimal number (floating-point or double-precision number),
or a string.
If you need to use many variables, you can declare more than one on the same line. To do this, separate them with a comma. Here is an example:
var FullName, EmailAddress;
var Address, City, State, ZIPCode;
One of the most common assignments you will hand to your scripts consists
various kinds of operators to respond to virtually any type of operation you need.