Share via


JScript Array Object

An Array object is a variable that groups related pieces of data. A unique number, called an index or subscript, references each piece of data in the array. To access the data stored in the array, the array identifier and the index are combined with the index operator "[]", for example, theMonths[0].

Creating an Array

To create a new array, use the new operator and the Array constructor. In this example, the array constructor is used to construct an array with length 12. Then, data is entered into the array.

var theMonths = new Array(12);
theMonths[0] = "Jan";
theMonths[1] = "Feb";
theMonths[2] = "Mar";
theMonths[3] = "Apr";
theMonths[4] = "May";
theMonths[5] = "Jun";
theMonths[6] = "Jul";
theMonths[7] = "Aug";
theMonths[8] = "Sep";
theMonths[9] = "Oct";
theMonths[10] = "Nov";
theMonths[11] = "Dec";

When you create an array using the Array keyword, JScript includes a length property, which records the number of entries. If you do not specify a number, the length is set to zero, and the array has no entries. If you specify a number, the length is set to that number. If you specify more than one parameter, the parameters are used as entries in the array. In addition, the number of parameters is assigned to the length property, as in the following example, which is equivalent to the preceding example.

var theMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

Array literals provide another technique for entering data in an array. For more information, see Array Data.

The Array object stores sparse arrays. That is, if an array has three elements that are numbered 0, 1, and 2, element 50 can exist without the presence of elements 3 through 49. JScript automatically changes the value of the length property when you add elements to an Array object. Array indices in JScript always start at 0, not 1, so the length property is always one greater than the largest index in the array.

Using Expando Properties of Arrays

Array objects, just as any other object based on the JScript Object object, support expando properties. Expando properties are new properties that you dynamically add and delete from an array, like array indices. Unlike array indices, which must be whole numbers, expando properties are strings. In addition, adding or deleting expando properties does not change the length property.

For example:

// Initialize an array with three elements.
var myArray = new Array("Hello", 42, new Date(2000,1,1));
print(myArray.length); // Prints 3.
// Add some expando properties. They will not change the length.
myArray.expando = "JScript";
myArray["another Expando"] = "Windows";
print(myArray.length); // Still prints 3.

Typed Arrays

Another faster way to create the theMonths array shown above is to create a typed (native) array, in this case, an array of strings:

var theMonths : String[] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

Elements of typed arrays can be accessed faster than elements in JScript array objects. Typed arrays are compatible with arrays in other .NET Framework languages and help provide type safety.

JScript Arrays objects are very flexible and great to use for lists, queues, stacks, and so on, but native arrays are much better for storing fixed-size items of the same type. In general, unless the special features of the Array object are needed (dynamic resizing and so on), typed arrays should be used.

All nondestructive JScript Array methods (methods that do not change the length) can be called on typed arrays.

See Also

Reference

Array Object

Other Resources

Intrinsic Objects