javascript for journalists


An array is a method of storing multiple values in a single variable.

var myarray = ["bob", "john", "peter", "sandy"];

Each value in an array is separated by a comma. The values can also have a unique data types. For example, some values can be numbers, while other values can be strings or booleans.

var myarray = ["joe", 47, false, true];

In order to retrieve any element in the array, we use something called the index number. It is important to note that the index of every array starts on zero.

var myarray = ["bob", "john", "peter", "sandy"];
console.log( myarray[2] );

In the above example, myarray[2] would retrieve the value "peter" because it is the third element in the array. (remember, we start on zero.) We can also cite the index in a number of ways. What are the values of the following variables?

var myarray = [
var x = 1;

myarray[ 1 + 2]

In the first example, myarray[1+2] would retrieve the "sandy" value, because it is the fourth element. In the second example, the myarray[x] would retrieve the "john" value, because it is the second element. This idea of being able to bring up a value becomes very useful later, especially when working with large datasets.

It's also important to note how the array was typed out on multiple lines. JavaScript doesn't care how the information is presented, as long as the opening and closing brackets match up correctly.

Changing Values Within an Array

You can change individual values within an array by referencing them by their index number. Consider the following:

var myarray = [

myarray[1] = "mary";

The "john" value no longer exists in myarray, because we've replaced it with "mary." Remember, john was located at index 1, which is the second value because indices start on zero.

Multidimensional Arrays

A multidimensional array sounds more complicated than it is. The idea is simple: An array within an array. Sometimes the key to grasping a multidimensional array lies in how it's typed out.

var my2Darray = [
	["bob", "alex", "joe", "wendy"],
	["teresa", "jodi", "sara", "peter"],
	["jack", 44, 90],
	[true, false]

The above array has four elements, each one is also an array. So how do we reference each item? We use indexes for both the main array, and each child array nested. For the above array:

my2Darray[0][1] //This would return "alex"
my2Darray[2][2] //This would return 90
my2Darray[3][0] //This would return true
my2Darray[2][3] //This is undefined, since there is no fourth element

We used the name 2D array because this is a two-dimensional array. It is possible to further have more arrays nested within these arrays, and so on. It begins to get very complex however. Generally, most arrays don't go too far beyond three dimensions.

More ways to write arrays

Arrays can be typed and declared in a number of ways. If you want to declare a variable as an array without assigning any values to it, you have to use a special statement called a constructor. Here are two ways to do this:

var myarray = new Array();
var myarray = [];

These are both functionally the same thing. Once you've declared your array, it is possible to change out or assign specific values at various indices.

var myarray = [];
myarray[3] = "john";
myarray[2] = 56;
myarray[102] = "Peter";

If there was already a value in those spots, it would be replaced.

When you reference an array item, it works just like any other variable. You can concatenate them, or use them in arithmetic functions, like so:

var myarray = [1, 2, 3, 4, 5, 6, 7];
var sum = myarray[2] + myarray[4];

The value of sum would be 8, since we are adding the values 3 and 5. Remember, indices start on zero.

Test yourself:

var myarray = ["Java", "learned", "I", "today", "Script"];
var indices = [3, 2, 1, 0, 4];

What is the value of the variable message?

var message = myarray[indices[0]] + " " + 
	 myarray[indices[1]] + " " + 
	 myarray[indices[2]] + " " + 
	 myarray[indices[3]] + myarray[indices[4]];

In the above example, we actually use an array to represent the index value of each array.