Search within TutorialsPark

Javascript Object Type : Array


To create array objects which is a collection of like elements


Javascript Object Type : Array

An array is defined as a collection of like elements or values, eg a collection of names, images, audio files etc.

But according to JavaScript Specifications set by ECMAScript, arrays are ordered list of data, that can hold any type of data in each of the slots assigned, i.e a number in the first, string in the second, video file in third and so on and so forth.

Each array element is accessed using an index value enclosed within a square bracket, also called as subscript. Eg : countries[0]= "India" , countries[1]= "Pakistan"

Arrays based on Index values are of two types : numeric arrays and associative arrays

numeric arrays : An array indexed using numbers.

Associative arrays : An array indexed using.

array datastructure

JavaScript Arrays: Creating an Array

In JavaScript, arrays can be created using three basic ways: Constructor Method and array literal .


JavaScript Arrays: Creating Arrays Using Constructor Method

A new keyword is used to create an array object on the fly, by calling the Array's object constructor function Array()

The constructor argument specifies the size of the array, if you know the number of items before.Items can also be passed directly into the array

If an array is created with a predefined length, but not values then the empty array elements are assigned the value undefined

Syntax: Javascript Arrays : Creating Arrays Using Constructor Method.

Example: Javascript Arrays : Creating Arrays Using Constructor Method.

Give it a TRY! » Note: Even if you omit the operator new, the result is the same.


Javascript Arrays: Creating Arrays using Literal Notation

Another easy and quick way to create an array is to use an array literal notation.

An array literal is specified by using square brackets and within them placing a comma-seperated list of array items. Else everything remains the same.

Syntax: JavaScript Arrays: using Literal Notation

Example: JavaScript Arrays : using Literal Notation

Give it a TRY! » Note: If values between the commas is not specified, then each item get the value undefined.


Javascript Arrays: Filling an array using a for Loop

An array can be filled by assigning values to it using a for loop, array index values begins at 0 and ends at n-1, where n is the number of items in the array.

The demo below uses for loop to populate the array, by using a for loop which loops till the value of index is less than the size of the array.

Example: JavaScript Arrays : using Literal Notation

Give it a TRY! » Note: To populate an array by accepting values from user, use the prompt alert window.


Javascript Arrays: Properties of the Array Object

An Array Object has three properties to describe itself: constructor , length and prototype. See the table below for details.

Table: JavaScript Arrays : Properties of an Array

Properties Description
Constructor To reference the array object Constructor
length To return the number of items in the array.
prototype To extend the definition of an array by adding more properties and methods to the array.

Example: JavaScript Arrays : Properties of an Array

Give it a TRY! » Note: Array length is set to 1 , all element with index value of 1 or greater are removed permanently .


Javascript Arrays: Associative Arrays

An associative array is something that uses a string as an index value instead of a number, here the index is called as the key while the value attached to it is called as value.

The demo below uses a for loop to iterate elements of the associative array.

Syntax: JavaScript Arrays: Associative Array

Example: JavaScript Arrays : Associative Array

Give it a TRY! » Note: Only string can be used as an index value in associative array.


Javascript Arrays: Multidimensional Arrays

A multidimensional array comprises of nested arrays, i.e arrays within arrays.

Consider, a two dimensional array, here each row is a new array, each element is referenced using two index values, one for the row and the other for the column.

Eg: In a two dimensional array the first element in first row is referenced using array_name[0][0], second element in first row using array_name[0][1] and so on.

Syntax: JavaScript Arrays: Multidimensional Arrays

Example: JavaScript Arrays : Multidimensional Arrays

Give it a TRY! » Note: The for loop displays elements in the array by moving from row to row downwards.