Changes

Jump to: navigation, search

JavaScript Arrays

7 bytes added, 15:34, 15 April 2009
no edit summary
</pre>
We can also specifiy specify the intial initial size of the array when we create it by passing through the number of elements that we require the array to hold as an argument to the Array() object constructor. For example, to intialize initialize an array to hold 7 items:
<pre>
</pre>
'''Note:''' An array will grow automatically as new items are added so it is not necessary to specifiy specify at creation time the number of elements you believe the array will need. The decision as to whether to pre-allocate the initial size of the array is typically one of memory and speed. A mission critical application that needs to store a large amount of data in an array may not want the delay associated with waiting for the size of the array to be increased each time a new item is added. In this situation the array would be pre-initialized by specifying an initial size. This then has the trade-off that a large amount of memory may be taken up and not used right away. In practice it is usually acceptable to let arrays grow as items are added.
== Initializing the Elements of an Array ==
Before looking at how to add items to an array it is important to understand how arrays are indexed. The first item in an array is item 0, the second is item 1 and so on.
There are a number of different ways to initialize an array. One way is to specify the intial initial items at creation time by passing through the items as arguments to the the ''Array()'' constructor:
<pre>
</pre>
Any time you need to change the value of an array elementt element the same approach applies. For example, to change the second element of the array from "Green" to "Yellow":
<pre>
</pre>
Often, however, it is necessary to access each element in an array. While writing code to access each item one by one is accepbtable acceptable for an array with one only a few items, this becomes difficult for larger arrays. Fortunately thre there are a couple of ways to ''loop'' through all the items in the array.
One way is to create a ''for loop'' (see [[JavaScript Flow Control and Looping]]) combined with the ''length'' property which contains the number of items in the specified array. The following example creates a for loop that will display each item in the array until the number of items in the array is reached:
<td>pop()<td>Deletes the last element added to the array - the opposite of push()</td>
<tr>
<td>push()<td>Adds a new element ot to the end of the array - opposite of pop()</td>
<tr>
<td>reverse()<td>Reverses the order of the elements in the array</td>
<td>shift()<td>Deletes the first element in the array and shifts the remaining elements down to fill the empty slot</td>
<tr>
<td>slice(''begin'', ''end'')<td>Returns a subsection of the array based on the begin and end index arguments. The ''end'' argument is optional. If ommited omitted the end of the array is assumed.</td>
<tr>
<td>sort()<td>Sorts the elements of an array into alpha or numerical order</td>
<td>splice()<td>Inserts and removes elements from an array</td>
<tr>
<td>unshift()<td>Adds an elemt element to the front of the array</td>
</table>
<th>Property<th>Description</th>
<tr>
<td>length<td>Contains the lenght length of the array</td>
<tr>
<td>prototype<td>Provides a mechanism for JavaScript developers to add their own properties to the array object</td>
== JavaScript Array Sorting ==
As an example of using the methods of the array object we can look at sorting the elememts elements in our ''myColors'' array. The following script For example:
<pre>
</pre>
The above script will output the colors in the order in which we added them to the array:
<pre>
</pre>
If we sort the array using the ''sort() '' method before printing out the element however:
<pre>

Navigation menu