Page 1 of 3
The Array Object
That is, an Array object looks like a standard linear array of the sort that you find in almost any language - but there are some things that make it behave in ways that you might not expect.
As in the case of most languages, the Array object is indexed by an integer. However, unlike most languages you don't have to specify the size of the Array before you start using it.
var myArray=new Array();
and so on. In this case you can select any element of the array by index e.g. myArray is "B".
You can also create an array object using an Array literal to initialize it. For example:
creates exactly the same array as the previous example.
is a common idiom used to create an uninitialized array.
As a third way of creating and initializing an Array object, you an also include the initial values in the constructor function:
var myArray=new Array("A","B","C");
All three methods tend to be used in practice. Which is preferable depends on circumstance, but there are advantages to the array literal notation.
The length of an Array
The length property looks a lot like declaring the size of the array, but it isn't quite the same as, say, the dimension specifier in a static language.
The first big difference is that length is updated as you use the array. That is, length is always one more than the largest index you have used.
For example, after:
length is set to 21 corresponding to the 21 elements from myArray to myArray . Notice that only element 20 has been initialized and all of the elements from myArray to myArray return undefined if accessed.
You can create an array and set its length using the constructor to specify the final element of the array. For example:
var myArray=new Array(10);
sets the length of the array to 11 i.e. elements 0 to 10. All of the elements are set to undefined.
Notice that this means you can't use the constructor method to create an array with a single element. You also can't write things like:
var myArray=new Array(1.5);
as this doesn't specify the length of an array.
You can also set the length of an array after it has been created by assigning directly to the length property. For example:
sets the length of the array to 20 with all of the elements still uninitialized.
If you set the size of an array to be larger than it currently is then no much actually happens. However, if you set the size of an array to be smaller than it currently is, then you lose elements. For example:
var myArray=new Array("A","B","C");
displays undefined because after setting the array's length to 1 only myArray is left.
Working with Array
Notice that you can refer to array elements that don't currently exist. That is, unlike in other languages, you don't have to "declare" the length of an array before you use it.
If you attempt to access an element that hasn't been initialized then undefined is returned. For example:
If you assign to an element that doesn't currently exist then that element is created. For example:
displays 10, but if you try to display myArray you will discover that its value is still undefined.
- When you create an array object you don't have to specify its length - elements are created as you store values in them.
- Any elements you access that you haven't stored a value in return undefined.
- The array keeps a record of "how long it is" in its length property.
- The length of an array is simply one more than the highest indexed element you have used.
- You can set the length property by direct assignment or via the Array constructor.