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.

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.