In typed arrays (also called native arrays) you define a data type which all of the elements have to comply with. (You can define a typed array of type Object to store data of any type.)
Note: Typed Arrays are not supported in the JavaScript survey engine.
You must define the number of elements. The only way to change the number of elements is by recreating the array. Trying to access elements outside the range of the indexes, will generate an error. Typed arrays, are dense, that is, every index in the allowed range refers to an element.
Use of typed arrays provides type safety and performance improvements compared to Untyped Arrays.
Declaring Typed Arrays
Typed arrays can be declared in three different ways. We will look at these by declaring and initializing the same typed array consisting of string values that holds the name of the days of the week in English.
The data type of the elements in a typed array is defined with the name of the data type (go to About Data Types for more information) followed by square brackets ([]).
One way of declaring a typed array is to list the elements separated by commas within square brackets (this is called an array literal) :
var weekday : String[] =
["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
You can also declare an empty array, and then use the new operator to define the size of it: Use normal assignments
var weekday : String[];
weekday = new String[7];
This can also be done in one operation:
var weekday : new String[7];To assign values to the elements of the array after declaring it as in either of the last two examples, you can use normal assignments, referring to each of the elements of the array:
weekday[0] = "Monday";
weekday[1] = "Tuesday";
weekday[2] = "Wednesday";
weekday[3] = "Thursday";
weekday[4] = "Friday";
weekday[5] = "Saturday";
weekday[6] = "Sunday";
It is also possible to declare multidimensional typed arrays and arrays of typed arrays. Refer to a JScript .NET reference for more information.