February 2, 2009
Variables
A variable can be defined as a temporary container for a
changing piece of information. This information can be of
any data type, size, or value within the limits of
JavaScript, including no data at all - it may simply exist.
A variable may contain any of the many JavaScript Object,
Methods, Properties, Arrays or values. Variables may be used
within functions and statements, of which there are two
broadly defined types.
The Scope of a Variable - Local Variable vs. Global Variable
A variable can be said to be a Local Variable if it is
defined within the space contained by a function. That is,
it must be declared within the opening and closing braces
that enclose the function, thus becoming part of that one
function. It is not available to any other function but that
one function, with no exceptions. The Local Variable may be
of any data type to be used within the function. It may be
any size, within JavaScript limits. In declaring a Local
Variable within a function, always use the var keyword to
avoid confusing the JavaScript engine.
A variable can be said to be a Global Variable if it is
declared outside of the bounds of a function. It must exist
on its own. In the declaration of a Global Variable, the
programmer is making available the information contained
within the variable to every function and statement within
that document, as well as the functions and statements
represented in an external JavaScript Library. It may be of
any data type that will be used within the applicable
functions.
It is entirely possible for two variables with different
contents, or even the same contents, to exist at the same
time providing one is a Local Variable and one is a Global
Variable. This case is normally used when the contents of
the variable will not change, and must be used within a
function as a Local Variable, then outside of a function as
a Global Variable.
Creating and Naming Variables
Creating a variable is perhaps the most basic use of the
powers of JavaScript. It is done with one statement that can
be as small or as large as you have a need for. The name of
the variable is typically something descriptive of its
content. This makes it easier for you to understand its
contents at a later date and time. It also makes it easier
for others to understand your approach to the given problem.
There are a few things to keep in mind while naming your
variable. The list below examines each.
- The first character of a valid variable name must be a
letter or an underscore. Numbers, punctuation marks and
special characters are not tolerated under any
circumstances.
- There can be any amount of alphanumeric characters
following the first character, not including punctuation
marks and special characters.
- All characters must conform to the basic definition of
the ISO-Latin-1 character set.
- The letters used may be upper or lower case letters,
although JavaScript does distinguish between the two cases.
For example, the variable named variableOne is different in
the eyes of JavaScript from the variable named variableOne.
Case matters when referencing variable names.
Keeping these simple rules in mind is clearly a very easy
thing to do when naming your variables. Once you have
discovered what data you'd like to store in a variable, it
is time to think about naming that variable. Creating the
variable is an easy task, of which there are four common
variations, shown below.
- var variableName, variableName, variableName; -
You can see in this syntax example that there are three
variables being declared at once. You may declare as many
variables as you have a need for on one line. Keep in mind
that the variables declared in this manor are without values
assigned to them. They are empty, and merely exist until you
stuff something into them.
- var variableName = value; - This syntax example
shows the creation of one variable. The variable has been
assigned an initial value with the value addition to the end
of the statement.
- variableName; - This example shows the creation
of a variable without the use of the var keyword. This is an
entirely legal thing to do in JavaScript, and saves typing,
albeit a small amount. It is recommended you use the var
keyword with every variable declaration in order to curtail
any problems you may have with variable scope definitions.
- variableName = value; - This example shows the
creation of a variable without the use of the var keyword.
This is an entirely legal thing to do in JavaScript, and
saves typing, albeit a small amount. It is recommended you
use the var keyword with every variable declaration in order
to curtail any problems you may have with variable scope
definitions. You can see that an initial value has been
assigned to the variable, which can be used at a later time.
You can see in the above list that the "var" keyword isn't
always necessary - JavaScript is considered to be a "loosely
typed" language. This means that there is a lot of
flexibility in the way you write your code. Notice also that
every way of declaring a variable ended with a semi-colon.
This too isn't absolutely necessary for JavaScript. The
JavaScript engine takes educated guesses concerning the
omitting of semi-colons, and is usually correct in its
assumptions - though not always. Ideally, you should
remember to end every line of code with a semi-colon. It is
a good programming convention that takes the guesswork out
of execution by the JavaScript engine. It is recommended you
use the semi-colons at the end of every line.
The below example shows the creation of four variables:
<SCRIPT LANGUAGE="JavaScript">
<! --
var globalOne = "This is ";
globalTwo = "a string";
function writeVariables() {
var localOne = " of text";
document.write(globalOne, globalTwo, localOne);
}
-- >
</SCRIPT>
The example shows the creation of a variable called
globalOne using the var keyword, the content of which is the
string "This is ". It is of the String data type because it
is enclosed within double quotes, as stated in an earlier
section. Next comes another variable called globalTwo that
doesn't use the var keyword, but is still a valid JavaScript
variable. It is given the value of "a string" which again is
a string value because of the nesting of the text within
double quotes. Then comes the function, named
writeVariables. In this function is the local variable
fittingly called localOne. It too contains a string value -"
of text". Notice that a space has been left at the front of
the text. JavaScript will honor one space appearing before
the contents of a string variable, but no more. That is, if
you put one space within the text at the beginning of the
string and tell the browser to write it, the browser will
write that one space. But if you insert more spaces than
one, JavaScript will disregard the rest. The document.write
statement is then used to reference, then write the contents
of the three variables to the browser screen (that's what a
document.write statement does - more later). The output of
the script looks as follows:
This is a string of text
There is more that you can do with variables, but for now
this example is just to give you an idea of how the variable
is declared, and a bit about the use of the variable. Almost
all of the coming discussions on the JavaScript language
involve the use of variables in one form or another, so
you'll learn how to think in terms of variables as you go.
You can see that there is nothing to declaring, naming, and
working with variables. The makers of JavaScript have made
it very easy on you, the budding web developer. Enjoy.
Stop by next week where we'll cover all the fun that arrays
can be.
Data Types - Strings
JavaScript Introduction
The JavaScript Array
The JavaScript Chronicles
JavaScript Introduction
Part 2: Data Types
Part 3: Arrays
Part 4: Operators
Part 5: Conditional Statements
Part 6: JavaScript Functions
Part 7: Pattern Matching - The RegExp Object
Part 8: Introduction to Server Side JavaScript
Part 9: Server Side JavaScript Mail Sending
Part 10: Server Side JavaScript and File Manipulation
Part 11: Working with Forms in JavaScript
Part 12: Getting to Know Dynamic HTML
|