Java Reference
In-Depth Information
something called a variable . Why is it called a variable? Well, perhaps because a variable can be
used to store temporary data that can be altered, or varied.
Another bonus of variables is that unlike permanent storage, which might be saved to disk or
magnetic tape, variables are held in the computer's memory. This means that it is much, much faster
to store and retrieve the data.
So what makes variables good places for temporarily storing your data? Well, variables have a
limited lifetime. When your visitors close the page or move to a new one, your variables are lost,
unless you take some steps to save them somewhere.
You give each variable a name so that you can refer to it elsewhere in your code. These names must
follow certain rules.
As with much of JavaScript code, variable names are case sensitive. For example, myVariable is not
the same as myvariable . You'll find that this is a very easy way for errors to slip into your code,
even when you become an expert at JavaScript.
Also, you can't use certain names and characters for your variable names. Names you can't use
are called reserved words. Reserved words are words that JavaScript keeps for its own use (for
example, the word var or the word with ). Certain characters are also forbidden in variable names:
for example, the ampersand ( & ) and the percent sign ( % ). You are allowed to use numbers in your
variable names, but the names must not begin with numbers. So 101myVariable is not okay, but
myVariable101 is. Let's look at some more examples.
Invalid names include:
with
99variables
my%Variable
theGood&theBad
Valid names include:
myVariable99
myPercent_Variable
the_Good_and_the_Bad
You may want to use a naming convention for your variables (for example, one that describes what
sort of data you plan to hold in the variable). You can notate your variables in lots of different
ways—none are right or wrong, but it's best to stick with one of them.
Today, the convention most JavaScript developers use is to simply give their variables descriptive
names. For example, a variable for a person's first name would be called firstName ; his account
number would be accountNumber . However, as long as the names you use make sense and are used
consistently, it really doesn't matter what convention you choose.
Search WWH ::




Custom Search