JS Basic
JS HOME
JS Introduction
JS How To
JS Where To
JS Variables
JS Operators
JS Functions
JS Conditional
JS Looping
JS Guidelines

JS Advanced
JS String Object
JS Array Object
JS Date Object
JS Math Object
JS Window
JS Form
JS Browser

Examples/Quiz
JS Examples
JS Quiz Test

References
JS Objects

Resources
JS Books

JavaScript Array Object

The Array object

An 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 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 zero. 

You create an instance of the Array object with the "new" keyword.

var family_names=new Array(5)

The expected number of elements goes inside the parentheses, in this case 5.

You assign data to each of the elements in the array like this:

family_names[0]="Tove"
family_names[1]="Jani"
family_names[2]="Ståle"
family_names[3]="Hege"
family_names[4]="Kai"

And the data can be retrieved from any element by using the index of the particular array element you want. Like this:

mother=family_names[0]
father=family_names[1] 


The Most Common Methods

Methods Explanation NN IE ECMA
length

Returns the number of elements in an array. This property is assigned a value when an array is created

3.0 4.0 1.0
reverse() Returns the array reversed  3.0 4.0 1.0
slice() Returns a specified part of the array 4.0 4.0  
sort() Returns a sorted array 3.0 4.0 1.0

Examples

Array
Arrays are used to store a series of related data items. This example demonstrates how you can make an array that stores names.

<html>
<body>
<script type="text/javascript">
var famname = new Array(6)
famname[0] = "Jan Egil"
famname[1] = "Tove"
famname[2] = "Hege"
famname[3] = "Stale"
famname[4] = "Kai Jim"
famname[5] = "Borge"

for (i=0; i<6; i++)
{
document.write(famname[i] + "<br>")
}
</script>
</body>
</html>

Array 2
This is another way of making an array that gives the same result as the one above. Note that the length method is used to find out how many elements the array contains.

<html>
<body>
<script type="text/javascript">
var famname = new Array("Jan Egil","Tove","Hege","Stale","Kai Jim","Borge")

for (i=0; i {
document.write(famname[i] + "<br>")
}
</script>
</body>
</html>