Variables are simply numbers, letters, or words. They
the only thing that you have to define is a variables name (number or text)
and its value.
name. For instance, if you wanted to set a variable for a number, such as
the number 16, the code would look like this:
A variable can also have a value that is a string of
text, such as:
|var text = "Santa Clause Is Coming To Town"
Note that anytime you use a string, the string must be
enclosed inside quotation marks. Also note that variable names can contain
numbers, but must always start with a letter. Our two variable names from
the samples above are number and text. The first letter in the name is lower
case, but if there are two words that are put together, such as numberText
the first letter of the second word is capitalized.
A variable needs more to make it do anything. In our example above, var number
= 16 doesn't actually do anything. It needs more code to make something happen
- to make the variable matter.
var number = 16;
var text = "Many years ago when";
document.write(text + " I was " + number);
In the sample above, we open the script with the
<script> tag. We then set our variable name, which is the word number.
We assign a value of 16 to that variable. Then, we write a variable named
text, which has a string value of Many years ago when. Note that each variable
value ends with that semicolon. That is very important. All elements must
be separated with the semicolon, or the script won't work.
an object (document) and a method (write), and then we tell it what to write.
Text + I Was (enclosed in quotation marks), + number. When the script runs,
it will assign the text variable where it is indicated and the number variable
where it is indicated as well, to create a sentence that says Many years
ago when I was 16.
This now takes us to Arrays. Arrays are also variables,
but they are more complex, meaning that they contain more data or values
than regular variables do. Arrays simply index values, always starting with
the number zero. Here is a sample of an Array, using the days of the
|var days = new Array()
days = "Sunday";
days = "Monday";
days = "Tuesday";
days = "Wednesday";
days = "Thursday;
days = "Friday";
days = "Saturday";
When you create a variable such as this, you are actually
creating an Array, and it is defined as a new Array object by putting new
Array() as the value of the variable. Arrays can hold as many values as you
like. To join two variables together, you use the plus sign.
The Basics of
Variables and Arrays
Functions and Methods
Math Object Reference
Date Object Reference
Web Development Tutorials
Sheets Tutorial: An Introduction to Cascading Style Sheets
Development: A step by step guide to developing a successful Internet
Codes Chart: Copy and paste HTML codes for your web page
Copy and paste special effect HTML codes for your web page
Tips: Tips, tricks, and special effect codes for your web page
Web Safe Color Chart: Hexadecimal and RGB Color Codes for your web page
Codes Chart: American Standard Code for Information
Interchange character codes chart