Javascript Arrays

Introductions

An array is simply a list of data contained in one variable and accessed by indices. An example of an array would be an array of ages containing 0, 1, 2, 3 etc. The array doesn’t have to be integers, it can be an array of strings, objects or even arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Create an array
var ages = new Array(1,2,4,99,54,23);
 
// Create another array
var names = new Array(“bill”, “bob”);
 
// Create a literal array
var games = [“minecraft”,”half life”,”freecell”];
 
// Add values to an empty array
var ages = new Array();
ages[0] = 1;
ages[1] = 2;
ages[2] = 9;

It is important to know that the array is actually an object that contains values. Because this type is actually an object there are properties that can be called to help handle the information inside of the array.

Helper Functions And Properties

In javascript, along with many other languages, there are functions built around arrays that will allow you, the developer, to better use them and be more efficient in your coding. The length property is one of the most commonly used properties when going through an array.

1
2
3
4
5
6
7
8
9
// Going through the items in an array.
// Create the array
var ages = new Array(1,2,3,5,7);
 
// Loop through using the length property
for(var x = 0; x < ages.length; x++) {
// Print out the items one by one with an alert.
<p dir="ltr">alert(ages[x]);</p>
}

Array push and pop are commonly used to add items to the array without having to get the position of where the item will be added yourself.

1
2
3
4
5
6
7
8
// Create an array with one item
var names = [“bob”];
 
// push an item onto the array.
names.push(“caleb”);
 
// the resulting array will now be the same as it would be if you created it like this.
var names = [“bob”,”caleb”];

Now we are going to pop an element off of the end of the array and return the item. This call not only returns the value but it will alter the array in memory changing the values within it.

1
2
3
4
5
// create a new array of names that we will play with
var names = [“chewbacca”,”yoda”,”skywalker”,”ben”];
var poppedValue = names.pop();
 
// As a result the names array will no longer have “ben” in it and the poppedValue will contain the string “ben”;

Two Dimensional Array

The concept of a 2D array can seem a little intimidating at first until you realize that you need to look at the data as columns and rows. Here is an example of what a 2D array would look like.

1
2
3
4
// Creating a 2D array
var x = new Array(0,1);
x[0] = new Array(0,0);
x[1] = new Array(0,0);

In order to access a property within the second array you will just need to fetch the value using the key.

1
2
// Display the item that is in the first column of the first row.
alert(x[0][0]);

Another way to create an array like this is to loop through the items upon creation.

1
2
3
4
5
6
7
8
9
10
11
//Create the array container
var names = new Array();
 
for(var x = 0; x &lt; 10; x++) {
names[x] = new Array();
// Go through the second dimension and set the values.
for(var y = 0; y &lt; 10; y++) {
// Assign the value of the item to bob.
names[x][y] = “bob”;
}
}

Note: in strongly typed languages an array must consist of the same data types. Since javascript is not a strongly typed language; the information stored can consist of mixed data types.

Related posts:

  1. Javascript Hoirzontal Menu
  2. Javascript Canvas: Basic Game Logic and Movement
  3. Java: Tutorial about Arrays vs Static Arrays
  4. Writing and reading objects and arrays to a binary file with java
  5. Decrease Javascript Load Times and Increase Script Speed
  • Guest

    This is a nice tutorial, but there is one problem.

    In the beginning, you said that an array is a list of variables of the same type; that is not true.

    An array can contain multiple variables of different types

  • CalebJonasson

    Thanks for the comment. The post has been updated to reflect your change suggestion and a note has been placed at the bottom of the article explaining that Javascript is not a strongly typed language.

    kudos