JavaScript Array Object
The Array object is used to store a set of values in a single variable name. Each value is an element of the array and has an associated index number.
You create an instance of the Array object with the “new” keyword. The following example creates two arrays, both of three elements:
var family_names=new Array(3)var family_names=new Array(“Tove”,”Jani”,”Stale”) |
You can refer to a particular element in the array by using the name of the array and the index number. The index number starts at 0.
If you create an array with a single numeric parameter, you can assign data to each of the elements in the array like this:
family_names[0]=”Tove” family_names[1]=”Jani” family_names[2]=”Stale” |
And the data can be retrieved by using the index of the particular array element you want. Like this:
mother=family_names[0] father=family_names[1] |
The Array object’s properties and methods are described below:
NN: Netscape, IE: Internet ExplorerProperties
Syntax: object.property_name
Property | Description | NN | IE |
---|---|---|---|
constructor | Contains the function that created an object’s prototype | 3 | 4 |
index | 4 | 5.5 | |
input | 4 | 5.5 | |
length | Represents the length of the array | 3 | 4 |
prototype | Allows addition of properties to an array | 3 | 4 |
Methods
Syntax: object.method_name()
Method | Description | NN | IE |
---|---|---|---|
concat() | Returns an array concatenated of two or more arrays | 4 | 4 |
join() | Joins all the elements of an array into a string separated by a specified string separator (comma is default) | 3 | 4 |
pop() | Removes and returns the last element of an array | 4 | 5.5 |
push() | Adds one or more elements to the end of an array and returns the new length | 4 | 5.5 |
reverse() | Reverses the order of the elements in an array | 3 | 4 |
shift() | Removes and returns the first element of an array | 4 | 5.5 |
slice() | Creates a new array from a selected section of an array | 4 | 4 |
splice() | Used to add and/or remove elements of an array | 4 | 5.5 |
sort() | Sorts the elements of an array | 3 | 4 |
toSource() | Returns the source code of the array | 4.06 | 4 |
toString() | Returns a string representing the specified array and its elements | 3 | 4 |
unshift() | Adds one or more elements to the beginning of an array and returns the new length | 4 | 5.5 |
valueOf() | Returns a primitive value for a specified array | 4 | 3 |