COM271, Week 10

Arrays, Date, Math, and Type-Related Objects

Syllabus | Table of Pages | Assignments | References and Useful Links

Arrays

From W3Schools, javascript arrays are defined and explained as

What is an Array?

An array is a special variable, which can hold more than one value, at a time.

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

var car1="Saab";
var car2="Volvo";
var car3="BMW";

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The best solution here is to use an array!

An array can hold all your variable values under a single name. And you can access the values by referring to the array name.

Each element in the array has its own ID so that it can be easily accessed.


Create an Array

An array can be defined in three ways.

The following code creates an Array object called myCars:

1:

var myCars=new Array(); // regular array (add an optional integer
myCars[0]="Saab";       // argument to control array's size)
myCars[1]="Volvo";
myCars[2]="BMW";

2:

var myCars=new Array("Saab","Volvo","BMW"); // condensed array

3:

var myCars=["Saab","Volvo","BMW"]; // literal array

W3Schools: "JavaScript Array Object"

Accessing Array Elements: Array elements in JavaScript are indexed beginning with zero. Using the myCars array (above), myCars[2] would return the value "BMW".

Adding, Changing, and Deleting Array Elements: JavaScript arrays can be expanded (new elements added) just by indexing the array and assigning a value, as
myCars[3] = "Chevrolet";
Array values do not have to be set contiguously. Values can be changed by assigning them, as
myCars[21] = 'Ford';

Elements can be removed with delete myCars[3] (removes 4th element from array).

Length: The length property returns the index of the next unfilled position at the end of the array, regardless of whether all elements in the array are filled. It is automatically incremented when elements are added to the array. Having added 'Ford' to myCars[21], above, myCars.length is now 22. Note that this does not mean that memory has been set aside for 22 elements. In JavaScript, memory is allocated only when elements have values. Here, we've filled (above) only myCars[0], myCars[1], myCars[2], myCars[3], and myCars[21].

Manipulating Arrays

Common operations are carried out with array methods:

  • splice() is used to add, replace, or remove elements from an array in place. Syntax is myArr ( start, deleteCount, replacevalues ), where start is the index where the operation begins, deletecount is the number of elements to be deleted starting with start, and replacevalues (comma separated if more than one) are inserted to replace deleted elements.
  • toString() returns a string containing comma-separated values of the array.
  • toSource() preserves square brackets in the string representation.
  • sort() sorts the array in place according to lexicographic order.
  • Multidimensional arrays: Although not part of the spec, most JavaScript browsers will support multidimensional arrays. Remember that each dimension has 0 as its first index.