JavaScript Array:

We have learned that a variable can hold only one value, for example var i = 1, we can assign only one literal value to i. We cannot assign multiple literal values to a variable i. To overcome this problem, JavaScript provides an array.

An array is a special type of variable, which can store multiple values using special syntax. Every value is associated with numeric index starting with 0. The following figure illustrates how an array stores values.

JavaScript Array

Array Initialization:

An array in JavaScript can be defined and initialized in two ways, array literal and Array constructor syntax.

Array Literal:

Array literal syntax is simple. It takes a list of values separated by a comma and enclosed in square brackets.

Syntax:
var <array-name> = [element0, element1, element2,... elementN];

The following example shows how to define and initialize an array using array literal syntax.

Example: Declare and initialize an array

var stringArray = ["one", "two", "three"];

var numericArray = [1, 2, 3, 4];

var decimalArray = [1.1, 1.2, 1.3];

var booleanArray = [true, false, false, true];

var mixedArray = [1, "two", "three", 4];

JavaScript array can store multiple element of different data types. It is not required to store value of same data type in an array.

Array Constructor:

You can initialize an array with Array constructor syntax using new keyword.

The Array constructor has following three forms.

Syntax:
var arrayName = new Array();

var arrayName = new Array(Number length);

var arrayName = new Array(element1, element2, element3,... elementN);

As you can see in the above syntax, an array can be initialized using new keyword, in the same way as an object.

The following example shows how to define an array using Array constructor syntax.

Example: Array constructor syntax

var stringArray = new Array();
stringArray[0] = "one";
stringArray[1] = "two";
stringArray[2] = "three";
stringArray[3] = "four";

var numericArray = new Array(3);
numericArray[0] = 1;
numericArray[1] = 2;
numericArray[2] = 3;

var mixedArray = new Array(1, "two", 3, "four");

Please note that array can only have numeric index (key). Index cannot be of string or any other data type. The following syntax is incorrect.

Example: Incorrect array index of string type

var stringArray = new Array();

stringArray["one"] = "one";
stringArray["two"] = "two";
stringArray["three"] = "three";
stringArray["four"] = "four";

Accessing Array Elements:

An array elements (values) can be accessed using index (key). Specify an index in square bracket with array name to access the element at particular index. Please note that index of an array starts from zero in JavaScript.

Example: Access Array Elements

var stringArray = new Array("one", "two", "three", "four");

stringArray[0]; // returns "one"
stringArray[1]; // returns "two"
stringArray[2]; // returns "three"
stringArray[3]; // returns "four"

var numericArray = [1, 2, 3, 4];
numericArray[0]; // returns 1
numericArray[1]; // returns 2
numericArray[2]; // returns 3
numericArray[3]; // returns 4

Array Properties:

Array includes "length" property which returns number of elements in the array.

Use for loop to access all the elements of an array using length property.

Example: Access Array using for loop

var stringArray = new Array("one", "two", "three", "four");

for (var i = 0; i < stringArray.length ; i++) 
{
    stringArray[i];
}

Points to Remember :

  1. An array is a special type of variable that stores multiple values using a special syntax.
  2. An array can be created using array literal or Array constructor syntax.
  3. Array literal syntax: var stringArray = ["one", "two", "three"];
  4. Array constructor syntax:var numericArray = new Array(3);
  5. A single array can store values of different data types.
  6. An array elements (values) can be accessed using zero based index (key). e.g. array[0].
  7. An array index must be numeric.
  8. Array includes length property and various methods to operate on array objects.