Variables
What are
Variables?
Variables are like storage
units. You can create variables to
hold values. It is ideal to name a
variable something that is
logical, so that you'll remember
what you are using it for. For
example, if you were writing a
program to divide 2 numbers, it
could be confusing if you called
your variables numberOne,
numberTwo, numberThree because you
may forget which one is the
divisor, which one is the
dividend, and which one is the
quotient. A more logical approach
would be to name them just that:
divisor, dividend, quotient.
It is important to know the
proper syntax to which variables
must conform:
- They must start with a
letter or underscore ("_")
- Subsequent characters can
also be digits (0-9) or letters
(A-Z and/or a-z). Remember,
JavaScript is case-sensitive.
(That means that MyVariable and
myVariable are two different
names to JavaScript, because
they have different
capitalization.)
Some examples of legal names are
Number_hits, temp99, and _name.
When you declare a variable by
assignment outside of a function,
it is called a global variable,
because it is available everywhere
in the current document. When you
declare a variable within a
function, it is called a local
variable, because it is available
only within the function. Using
var is optional, but you need to
use it if you have a variable that
has been declared global and you
want to re-declare it as a local
variable inside a function.
Variables can store all kinds
of data (see below, Values of
Variables, section 3.2). To assign
a value to a variable, you use the
following notation:
dividend = 8;
divisor = 4;
myString = "I may want to use
this message multiple times";
message = myString;
Let's say the main part of the
function will be dividing the
dividend by the divisor and
storing that number in a variable
called quotient. I can write this
line of code in my program:
quotient = divisor*dividend, and I
have both stored the value of the
quotient to the variable quotient
and I have declared the variable
at the same time. If I had wanted
to, I could have declared it along
with my other assigned variables
above, with a value of null. After
executing the program, the value
of quotient will be 2.
It is important to think about the
design of your program before you
begin. You should create the
appropriate variables so that it
makes your life easier when you go
to write the program. For
instance, if you know that you
will be coding a lot of the same
strings in a message, you may want
to create a variable called
message and give it the value of
your message. That way, when you
call it in your program, you do
not have to retype the same
sentence over and over again, and
if you want to change the content
of that message, you only have to
change it once -- in the variable
declaration.
Values of
Variables
JavaScript recognizes the
following types of values:
-
Numbers, such as 42 or 3.14159
-
Logical (Boolean) values, either
true or false
-
Strings, such as "Howdy!"
-
null, a special keyword which
refers to nothing
This relatively small set of types
of values, or data types, enables
you to perform useful functions
with your applications. There is
no explicit distinction between
integer and real-valued numbers.
Data Type
Conversion
JavaScript is a loosely typed
language. That means you do not
have to specify the data type of a
variable when you declare it, and
data types are converted
automatically as needed during
script execution. So, for example,
you could define a variable as
follows:
var answer = 42
And later, you could assign the
same variable a string value, for
example,
answer = "Thanks for all the
fish..."
Because JavaScript is loosely
typed, this assignment does not
cause an error message. However,
this is not good coding! You
should create variables for a
specific type, such as an integer,
string, or array, and be
consistent in the values that you
store in the variable. This
prevents confusion when you are
writing your program.
In expressions involving
numeric and string values,
JavaScript converts the numeric
values to strings. For example,
consider the following statements:
x = "The answer is " + 42
y = 42 + " is the answer."
(The + sign tells JavaScript to
concatenate, or stick together,
the two strings. For example, if
you write:
message = "Hello" + "World"
...then the variable message
becomes the string "Hello World")
In the first statement, x
becomes the string "The answer is
42." In the second, y becomes the
string "42 is the answer."
|