JavaScript Programming

Create an array with a numeric parameter and assign data to it
Create an array and assign data to it.

Create a literal array
Create a literal array. Note that the length method is used to find out how many elements the array contains.

Array - some properties and methods
Some properties and methods to use with the Array object.

Array - sort()
The sort() method. What happens if we want to sort an array that consists of numbers? What is the sort order?

Array - concat() and slice()
How to use concat() and slice().

Array - splice()
How to use the splice() method.


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 number 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 Explorer

Properties

Syntax: object.property_name

Property Description NN IE 
constructor Contains the function that created an object's prototype 4 4
length Returns the number of elements in the array 3 4
prototype Allows you to add properties to an array 3 4

Methods

Syntax: object.method_name()

Method Description NN IE
concat() Joins two or more arrays and returns a new array 4 4
join(delimiter) Puts all the elements of an array into a string separated by a specified delimiter (comma is default) 3 4
pop() Removes and returns the last element of an array 4 5.5
push("element1","element2") 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(begin[,end]) Creates a new array from a selected section of an existing array 4 4
sort() Sorts the elements of an array 3 4
splice(index,howmany[,el1,el2]) Adds and/or removes elements of an array 4 5.5
toSource() Returns a string that represents the source code of the array 4.06 4
toString() Returns a string that represents the specified array and its elements 3 4
unshift("element1","element2") Adds one or more elements to the beginning of an array and returns the new length 4 5.5
valueOf() Returns the primitive value of an array 4 3