A variable is basically a "container" in which you can store information.
If you compared it to a cup for a second, a cup could contain any amount of any
type of liquid, or it could contain no liquid at all. The value stored within
this variable, or container however, can be changed during a script. You can
refer to a variable by name in order to either see its value or if you want to
change it. A given script can have an unlimited number of variables as long as
each has a unique name.
Variable Naming Rules
- Variable names are case sensitive, so Coke, CoKe and
coke are three different variables. This becomes important
to get in the habit of standardizing how you name your
variables.
- Variable names must start with an underscore or a
letter, so coke and _coke and coke0 are all valid variable
names where as 0coke or 123_coke are not valid variable
names. Remembering these rules will save you a lot of
trouble in scripting.
- When naming your variables, you have to remember that
you can NOT use math symbols. These include things like , "
' . () ; etc.
- There are reserved words in JavaScript, words that are
used in the language, reserved words that you cannot use the
name your variables, the list is very long and you're
welcome to look it up but a few examples are (new, do, else,
if, import, this, switch)
Types of Variables
- Integer Numbers
Integer variables can hold the basic counting numbers, either positive or
negative. These values can be operated on using arithmetic operators such as
addition, subtraction, multiplication, and division.
Example: 4, 564, -34
- Floating-Point Numbers
Floating-point variables can hold fractional numbers that include a
decimal point. These variables can also be used with the standard arithmetic
operators.
Example: 4.5, 9.876, -0.93
- String Variables
String variables are simply words and sentences. Always use quotation
marks with string variables.
Example: "hello world", "this is a variable"
- Boolean Variables
Boolean variables are a unique variable type. They can only hold the
values true and false. These variables are most used to test
the condition of a statement. These variables can also hold the values 0 and
1, for false and true, respectively.
Example: True, False, 1, 0
Declaring a Variable
You create a variable by using the var statement, and then afterwards listing
the name of the variable you want to use, using the naming conventions listed
above. It is also possible to declare a variable without the var statement but
it is a good habit to use it. See Figure 2.1 for an example of declaring
the variable email and setting its value to
zexipher@yahoo.com.
Figure 2.1 |
var email = "zexipher@yahoo.com" |
Lifetime of Variables
Each variable that you declare within a function can only be accessed from
within that function. After the function is left the variable no longer exists.
It is only recreated after you next access the function, but with the default
value. Not any changes that you may have made to the variable during prior uses.
These variables (variables you declared within a function) are called local
variables. You can actually use the same name for two local variables if they
are used in separate functions because each variable is only recognized by the
function in which it was declared.
If you declare a variable outside of a function on your page, it is called a
global variable. This means that the lifetime of the variable starts when you
open the page and ends when the page is closed.
Code Documentation
While in very simple scripts it is very easy to keep track of all of your
variables, especially if given relevant names. Using code documentation
can sometimes work to your advantage. This means using comments to explain each
variable you declare within your function. For examples of how to use comments
in JavaScript see Figure 2.2 below. Only a very simple explanation is
needed but is useful, especially if you intend to share your work with others.
See Figure 2.3 for an example of code documentation. This is not required
in JavaScript but simply a useful tool for future users of your scripts.
Figure 2.2 |
// this is a one
line comment /* this is a multiple
line comment */ |
Figure 2.3 |
var height = 72;
//customer height in inches
var weight = 210; //customer height in lbs
var bench = 150; //how many lbs the customer can bench |
|