An array collects more than one piece of data in one variable. A single index number (for a one-dimensional array) or several index numbers (for an array of arrays or a multidimensional array) reference the data in the array. You can refer to an individual element of an array with the array identifier followed with the array index in square brackets (). To refer to the array as a whole, just use the array identifier. Collecting data in arrays simplifies data management. For example, by using an array, a method can pass a list of names to a function using only one parameter.
There are two types of arrays in JScript, JScript arrays and typed arrays. While the two types of arrays are similar, there are a few differences. JScript arrays and typed arrays can interoperate with each other. Consequently, a JScript Array object can call the methods and properties of any typed array, and typed arrays can call many of the methods and properties of the Array object. Furthermore, functions that accept typed arrays accept Array objects, and vice versa. For more information, see Array Object.
Typed arrays (also called native arrays) are similar to arrays used in languages such as C and C++. Typed arrays provide type safety by storing only data that corresponds to the type that the array type declaration specified.
You can define a typed array of type Object to store data of any type.
When the script creates or initializes the array, it sets the number of elements in a typed array. The only way to change the number of elements is to recreate the array. A typed array created with n elements has elements numbered 0 through n-1. An attempt to access elements outside that range generates an error. In addition, typed arrays are dense, that is, every index in the allowed range refers to an element.
A script can assign a declared, typed array to a variable or constant, or it can pass the array to a function, operator, or statement. When assigning to a variable (or constant), make sure that the data type of the variable matches the type of the array and that the dimensionalities of the arrays match.
A typed array is an instance of the .NET Framework System.Array object. To access static members of the System.Array object or to explicitly create a System.Array object requires the fully qualified name System.Array. This syntax distinguishes it from Array, the intrinsic JScript object.
A JScript Array object, which provides more flexibility than a typed array, is convenient when you want a generic stack, when you want a list of items, and when performance is not a top concern. However, since typed arrays provide type safety, performance improvements, and better interaction with other languages, developers typically choose typed arrays instead of JScript arrays.
A JScript array can store data of any type, which makes it easy to quickly write scripts that use arrays without considering type conflicts. Since this bypasses the strong type checking that JScript provides, use this feature carefully.
Scripts can dynamically add elements to or remove elements from JScript arrays. To add an array element, assign a value to the element. The delete operator can remove elements.
A JScript array is sparse. 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. Each JScript array has a length property that is automatically updated when an element is added. In the previous example, the addition of element 50 causes the value of the length variable to change to 51 rather than to 4.
A JScript Array object and a JScript Object are almost identical. The two main differences are that an Object (by default) does not have an automatic length property, and a JScript Object does not have the properties and methods of an Array. For more information, see JScript Array Object.