Pilcrow Records

Scripter Javascript Tutorial

javascriptmusiclogicscripterbookexcerpttutorial

42 Create an Array

The most common way to declare an array is assigning the variable with a pair of empty brackets, also known as the object literal because the use of array is almost always indicated by brackets. This creates an array that is considered empty, meaning it has no slots in which to save data:

var scale = [];

Another way to declare an array is assigning the variable with data between the brackets separated with commas:

var scale = [60, 62, 64, 65, 67, 69, 71];

In the above example, an array is declared with all of the MIDI pitches of the C Major scale starting at Middle C. The same can be done with other data types like strings:

var scale = ["do", "re", "mi", "fa", "so", "la", "ti"];

The third way to declare an array is to use the new keyword. An array is an Object, a special data type which has special properties.

var scale = new Array(7);

In the above example, this declares an array with 7 addresses, but the contents are undefined.

As noted before, arrays can store multiple kinds of data in an array.

var array = ["C", 60];
Trace(array); // C,60