To create array objects which is a collection of like elements
An array is defined as a collection of like elements or values, eg a collection of names, images, audio files etc.
Each array element is accessed using an index value enclosed within a square bracket, also called as subscript. Eg : countries= "India" , countries= "Pakistan"
Arrays based on Index values are of two types : numeric arrays and associative arrays
numeric arrays : An array indexed using numbers.
Associative arrays : An array indexed using.
A new keyword is used to create an array object on the fly, by calling the Array's object constructor function Array()
The constructor argument specifies the size of the array, if you know the number of items before.Items can also be passed directly into the array
If an array is created with a predefined length, but not values then the empty array elements are assigned the value undefined
Another easy and quick way to create an array is to use an array literal notation.
An array literal is specified by using square brackets and within them placing a comma-seperated list of array items. Else everything remains the same.
An array can be filled by assigning values to it using a for loop, array index values begins at 0 and ends at n-1, where n is the number of items in the array.
The demo below uses for loop to populate the array, by using a for loop which loops till the value of index is less than the size of the array.
An Array Object has three properties to describe itself: constructor , length and prototype. See the table below for details.
|Constructor||To reference the array object Constructor
|length||To return the number of items in the array.
|prototype||To extend the definition of an array by adding more properties and methods to the array.|
An associative array is something that uses a string as an index value instead of a number, here the index is called as the key while the value attached to it is called as value.
The demo below uses a for loop to iterate elements of the associative array.
A multidimensional array comprises of nested arrays, i.e arrays within arrays.
Consider, a two dimensional array, here each row is a new array, each element is referenced using two index values, one for the row and the other for the column.
Eg: In a two dimensional array the first element in first row is referenced using array_name, second element in first row using array_name and so on.