Arrays in Java script

Arrays are one way of keeping a program more organized. They allow you to do some things that are difficult without them. Arrays are usually a group of the same variable type that use an index number to distinguish them from each other. Suppose you wanted to write out 5 names, and use variables for each one of the names. You could define 5 variables:

However, a better way may be to use an array. To define an array, we need to follow the following form:

var Name_of_Array= new Array(number_of_elements);

Name_of_Array would be the name you want to give the array variable, the number_of_elements is the number of variables you want the array to store. So, for our names, we could write:

Code: JavaScript

varname= new Array(5);

Now, to assign values, we are going to use what is called an index number, and place it inside brackets. We attach this right on the end of the word name, so our first name would be:

Code: JavaScript

name[0]

I know, zero instead of 1. Arrays just work that way, the first element is always zero and the last element is always one less than what you define as the number of elements. In this case, our first element is name[0], the last is name[4]. So to assign the values we had, we could use a straight assignment, like this:

Notice that when we do this, we leave off the semicolon at the end of the array definition, and use semicolons at the end of each element we define. So far, it has only made things more complicated. However, this array can now be used as part of a loop, using the value of the index numbers as a variable:

Code: JavaScript

var x=0; for(x=0; x<5; x++){alert(name[x]); }

This uses the variable x in place of each index number, and runs the alert for all 5 elements in our array. The first time through, you would get an alert with name[0], the second time, name[1], and so on..

x++ means that we add one to the value of x. In a similar fashion, we can subtract one from x with x--. This is how we are able to go from x being 0 to x being equal to 4. It stops at 4 because part of the condition is that the loop is only run while x is less than 5. So the last time it runs, x is equal to 5 and it does not go any further.

Notice that we just defined the array, without assigning initial values. Don't forget the semicolon! Then we get the values by letting the viewer enter them through a prompt. name[y] uses the value of y each time it is run. So, you are assigning name[0] the first time, name[1] the second time, and so on. The complete script is below

Associative arrays give you another way to store information. Using associative arrays, you can call the array element you need using a string rather than a number, which is often easier to remember. The downside is that these aren't as useful in a loop because they do not use numbers as the index value. An associative array is defined just like a regular array, but we insert some text in the place we had numbers in the regular arrays:

Now we can get the Word Zen later in the script by remembering it is the "cool" car, so we would use my_cars["cool"]. This could be used to prompt a user to input the type of car he/she would prefer. Then you can send back what you think the viewer should buy based on the input:

Arrays in JavaScript are extremely useful for storing and manipulating information you have coded directly into the script, or information collected from the browser. In this article, Dan Wellman details how to use and manipulate them.
The array is one of a number of objects built directly into JavaScript. Think of them simply as variables containing multiple values. They can hold string or numerical values, have no maximum boundaries (although the speed at which your scripts are interpreted will decrease as the amount of data increases) and are comma delimited. They have no persistence, meaning that their values are not held once the page containing the script has closed or reloaded.

The aim of this article is to show you how information can be hard-coded into your scripts and how that information can be addressed and used to enhance your Web pages. The example script will focus just on one dimensional arrays.

Single Dimensioned Arrays

A simple example of a single-dimensioned array is:

myArray = new Array(15,89,4,61,5)

Which simply says create an array called myArray with five values, the values are integers and equal 15, 89, 4, 61 and 5. This information, were it to mean anything, could then be manipulated in all kinds of ways; you could print the results on the page using the simple command:

document.write(myArray[1])

That would display 15 on the page. You could also perform simple math, were the need to arise:

var mySum = myArray[1] * myArray[2]
document.write(mySum)

which displays the number 356.

An important property of the array object is the length property; the extremely basic numerical array above has a length of five. Each of the values in the array can be accessed using their index within square brackets following the array name, for example:

myArray[2]

It’s important to remember that the indices of any array always begin with 0 representing the first item in the array; therefore, while the length property of this very simple array is five, the highest index is four.