var number = 16
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.
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 week.
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.