JavaScript Variables and Arrays

 

JavaScript Variables and Arrays

This is part five of the JavaScript tutorial. If you missed the previous lessons, make sure you complete them prior to viewing this JavaScript Variables and Arrays tutorial.

Variables are simply numbers, letters, or words. They are used to connect to the value that is assigned to the variable. In JavaScript, the only thing that you have to define is a variables name (number or text) and its value.

In JavaScript code, a variable is used as var. Var is followed by the variables name. For instance, if you wanted to set a variable for a number, such as the number 16, the code would look like this:

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.

Let’s make JavaScript print something on our page using a variable.

<script type="text/javascript">
var number = 16;
var text = "Many years ago when";
document.write(text + " I was " + number);
</script>

 
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.

Next, we use some JavaScript that you have come to know and love. We assign 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 week.

var days = new Array()
days[0] = "Sunday";
days[1] = "Monday";
days[2] = "Tuesday";
days[3] = "Wednesday";
days[4] = "Thursday;
days[5] = "Friday";
days[6] = "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.

 
This concludes the JavaScript Variables and Arrays lesson. In the next lesson, we will focus on JavaScript Statements.