JavaScript Array


JavaScript array is an object that represents a collection of similar type of elements which are used to hold multiple values in a single variable.

An array can only have a numeric index (key). It’s index cannot be of string or any other data type.

Array index starts from 0.

We can get the length of an array using its length property.

We can easily create an array using any of the following methods:

1. By an array literal
2. By creating an instance of Array
3. By using an array constructor


1. Creating an array by an array literal

Syntax:

    var arrayName = [value1, value2,..., valueN];


Example:

    var students = [“Abhishek”, “SRK”, “Dhoni”];



2. Creating an array by instance of Array

Syntax:

    var arrayName = new Array();


Example:

    var students = new Array();
	students[0] = “Abhishek”;
	students[1] = “SRK”;
	students[2] = “Dhoni”;


3. Creating an array by array constructor

Syntax:

    var arrayName = new Array (“value1”, “value2”,..., “valueN”);


Example:

    var students = new Array (“Abhishek”, “SRK”, “Dhoni”);