shadowline
Functions << Arrays >> Loops
Arrays are used to organize a set of variables.

Here is an example of a regular script defining 10 names using variables.

<script type="text/javascript">
<!--
var a="Joe";
var b="Peter";
var c="Mary";
var d="Chris";
var e="Dave";
var f="Sally";
var g="Pat";
var h="John";
var i="Larry";
var j="Andrew";
// -->
</script>

This can become a very hard task when you have to remember which variable is holding which name. Not to mention you will have to think of a different VAR name for each new value. The example shows simple letters being used, but any word can be used. This may prove to be even more confusing and hard to remember. Especially if there are many more names than 10.

The solution to this is organizing using arrays. Here is the example above using an array format.

<script type="text/javascript">
<!--
x=new Array( );
x[0]="Joe";
x[1]="Peter";
x[2]="Mary";
x[3]="Chris";
x[4]="Dave";
x[5]="Sally";
x[6]="Pat";
x[7]="John";
x[8]="Larry";
x[9]="Andrew";
// -->
</script>

Or you can condense this same array into :

<script type="text/javascript">
<!--
x=new Array("Joe","Peter","Mary","Chris","Dave","Sally","Pat","John","Larry","Andrew");
// -->
</script>

OK, it looks like a bit more typing is involved here, but there are some advantages.

  • You don't have to think of separate names for each value.
  • It is now easier to track down and reference the values.
new Array prepares a section of cells. The number of cells is given with in brackets. So the example is creating a section of 10 cells called named x.

The next step is filling in the values for the x array. JavaScript is very literal. It likes to start counting at 0. So entering the values for the array must start at the 0 mark.

x[0]="Joe";

And so forth down the array until all the cells are filled.

Now that you have all that information stored, you can use it in your coding applications. For example, if you needed to show the name of the 4th person in your list :

<script type="text/javascript">
<!--
document.write(x[3]);
// -->
</script>

See the example in action

The 3rd value was called. Since the array counting starts at 0 instead of 1, you have to subtract 1 from the number you want to find. A bit confusing, but easily remembered.

That shows how to do a simple look up this spot and print it out on the screen. How about a bit more interactive approach? Have the visitor enter a number and then have it print out the name that belongs to it.

<script type="text/javascript">
<!--
var GetNumber=prompt("Enter a number between 1 and 10","");
var TrueNumber = GetNumber - 1;
document.write(x[TrueNumber]);
// -->
</script>

  1. A PROMPT box appears asking for a number between 1 and 10. This number is placed in a cell named GetNumber. (If any other number is entered, the result states undefined).
  2. A cell called TrueNumber holds the value of GetNumber minus 1. Again, this is to get the correct cell reference as JavaScript starts counting at 0.
  3. The WRITE command then prints out the value for the stated array number.
See the example in action



NOTES : When using a arrays, CaPiTaLiZaTiOn is IMPORTANT! For example, if you code new array instead of new Array, you will get an error. Yes, it is that picky.

Stating the values for the arrays and declaring the array does not use var.
Functions << Arrays >> Loops
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow