An untyped Array object provides more flexibility than a typed array. It can store data of any type, which makes it easy to quickly write scripts that use arrays without considering type conflicts. Scripts can dynamically add elements to or remove elements from untyped arrays. To add an array element, assign a value to the element. The delete operator can remove elements.
An untyped 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 untyped 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.
Declaring Untyped Arrays
We will demonstrate the different ways of declaring and initializing untyped arrays using the same example as in the previous parts of the documentation: An array containing the names of the seven weekdays in English.
First, declaring it using an array literal, listing the elements within square brackets, separated by commas:
var weekday =
["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
Another way is to use the new operator to create a new Array object by using the new operator, the object name (Array) and then listing the elements of the array:
var weekday = new
Array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
You can also define an empty array:
var weekday = new Array();or an array with size 7:
var weekday = new Array(7);and then assign values to the elements:
weekday[0] = "Monday";
weekday[1] = "Tuesday";
weekday[2] = "Wednesday";
weekday[3] = "Thursday";
weekday[4] = "Friday";
weekday[5] = "Saturday";
weekday[6] = "Sunday";
The difference between declaring the array as an untyped array instead of as a typed array is that when it is defined as a typed array, all elements in the array must be of that type.
Also, in an untyped array you can insert elements of any type, including complex types such as other arrays or objects.
Another difference is that in an untyped array you can at any time add new elements, for example another weekday. This can be done no matter how the untyped array is defined, even when defined with the length specified, as in the example with new Array(7).
weekday[7] = "Lastday";(this would be a good idea to be able to at least occasionally finish projects before deadlines). The array has now 8 elements (0-7). In typed arrays, you cannot add new elements to the array, just modify existing elements.
And finally, in an untyped array you can have "holes". You can for example add a 50th element (with index 49):
weekday[49] = "Extremelyoverdueday";without defining all the elements in between. The size of the array will be 50, even though elements with index 8,…,48 have not been defined.