JavaScript Array

JavaScript array is a collection of values that are belonging to a same data type. You can store a list of values in single variables.

JavaScript array values you can access by array indexing. 0 index starting from the first value of the array.

List of array values that are separated by comma and must have to belong between opening and closing bracket.

Notes JavaScript Array use numeric indexes where as Object use numeric as well as string name. In the next lesson, we will learn more about Object.

JavaScript Array


Array syntax has three different forms, you can use any one to create an Array.

my_array = [value1, value2, ..., valueN];
my_array = new Array(value1, value2, ..., valueN);
my_array = new Array(array_size);


  • my_array: Required, Assign array values to this specified variable name.
  • value: Optionally, list of array values separated by comma.
  • array_size: Optionally, specifies an array size. This size indicates you'll store a maximum size of values. If the specified array size is not a valid allowed range, It'll throw the RangeError exception.

Create an JavaScript Array

Array Literal Method You can use the array literal method to create an array. Specifies an array items, and assign to a variable.

var random_list = [5, 12, 8, 22, 2, 30];
var domain = [".com", ".net", ".org", ".gov"];

An array literal method to create an array and execute very quickly.

JavaScript Built-in Array Constructor Method You can also use built-in array constructor new Array() method to create an array. Specifies an array items, and assign to a variable.

var random_list = new Array(5, 12, 8, 22, 2, 30);
var domain = new Array(".com", ".net", ".org", ".gov");

Avoid using the built-in array constructor method, use the array literal method would be good.

JavaScript size of array length If you pass only one numeric argument to the Array constructor, the arguments assume the length of the array. Later you can assign values to an array variable with array indexes.

var domain = new Array(10);     // an empty array of length 10

Find the length of Array Array.length property to find the length of an array,

domain.length                   // return 10

Run it...   »

domain[0] = ".com";
domain[1] = ".net";
domain[2] = ".org";
domain[3] = ".gov";

If you call an array constructor without any arguments, an array length set to zero.

var domain = new Array();       // an empty array, length 0
domain.length                   // return 0

Access an array elements: Access selected elements of an array, use indexing. Index value 0, starting from the first value of the array.

document.writeln( domain[0] );                   // return .com
document.writeln( domain[1] );                    // return .net
document.writeln( domain[1 + 1] );                // return .org
document.writeln( domain[domain.length - 1] );    // return .gov

Run it...   »

Looping through access array element A for statement iterates over of an array,

var domain = new Array(".com", ".net", ".org", ".gov");

for(var i = 0; i < domain.length; i++){
    document.writeln( domain[i] );

Run it...   »

for (variable in object) statement You can also use statement to iterates over of an array,

var domain = new Array(".com", ".net", ".org", ".gov");

for(var temp in domain){
    document.writeln( domain[temp] );

Run it...   »

Change the value in an array list You can modify/reassign the values of an array element using element index,

domain [3]=".me";

Delete an array element The delete statement to delete an array element from an array. Actually deleted statement not delete an array element, is just set undefined.

var domain = new Array(".com", ".net", ".org", ".gov");
domain[4] = ".int";                // add new array element
document.writeln( domain );
delete domain[4];                  // return true
document.writeln( domain[4] );     // undefined

document.writeln( domain );
document.writeln( "length: " + domain.length ); 
                                   // length 5
console.log( domain );             // [".com", ".net", ".org", ".gov", undefined × 1]

Run it...   »

delete statement is not perfect for removing completely, you have to use splice() method.

JavaScript splice() method modifying array elements, you can remove existing element, adds new elements.

var domain = new Array(".com", ".net", ".org", ".gov");
domain.splice(4, 0, ".int");            // add new elements on indexes 4th 
document.writeln( domain );             // [".com", ".net", ".org", ".gov", ".int"]

domain.splice(4, 1);                    // remove one elements after indexes 4th
document.writeln( domain );             // [".com", ".net", ".org", ".gov"]

Run it...   »

JavaScript haven't associative Array?

As we see above array use numeric indexes. Many programming languages have an associative array concept. But in JavaScript array use only numeric indexes. However, you can use associative key value using JavaScript object. That we'll learn in next lesson.