JavaScript Arrays

In JavaScript, arrays is an object that is capable of storing a sequence of value. Its is a set of variables with the same name.

The variable items in an arrays are called element of array, element are indicated by an index,

The length of an arrays in the number of element that are contained in an array. Array element index starts with Zero (0).

Syntax for creating an arrays is:

Array in JavaScript are simple built in object and created by using new keyword.

var arrayname = new array ()
var arrayname = new array (array length)

Here first example create an array of size zero and second specified the size of array.

Example of array declaraction are:
var day = new array (31);
var month = new array (12);
var n = new array (10, 20, 30, 40);
Creating array by simply assigning value

The individual element of an array accessing by using the name of an array by the index value of the array element enclosed in square brackets [].


var i = ["apple","orange","mango"];
Here i has three element.
i [0] is the first element.
i [1] is the second element.
i [2] is the third element.

JavaScript supports array of element data different types. these element can also refer to other array.

 i = new array ("ab",2,true,new array(1,3));
Here i has four element.
i[0] = "ab"
i[1] = 2
i[2] = true
i[3] = 1,3.
Here last element i[3] of array i is a dense array with two values 1 and 2. These two element can be accessed using a second set of subscripts as:
n1 = i[3][0];
n2 = i[3][1];

Leave a Reply