Last modified on 25 March 2015, at 14:19


Previous: Dates Index Next: Regular Expressions

An array is a type of variable that stores a collection of variables. Arrays in JavaScript are zero-based - they start from zero. (instead of foo[1], foo[2], foo[3], JavaScript uses foo[0], foo[1], foo[2].)

Basic useEdit

To make a new array, make a variable and give it a value of new Array().

var foo = new Array()

After defining it, you can add elements to the array by using the variable's name, and the name of the array element in square brackets.

foo[0] = "foo";
foo[1] = "fool";
foo[2] = "food";

You can call an element in an array the same way.

alert(foo[2]);	// outputs "food"

You can define and set the values for an array with shorthand notation.

var foo = ["foo", "fool", "food"];


Make an array with "zzz" as one of the elements, and then make an alert box using that element.

var array = ["anton", "bertha", "caesar", "zzz"];

Nested arraysEdit

You can put an array in an array.

The first step is to simply make an array. Then make an element (or more) of it an array.

var foo2 = new Array();
foo2[0] = new Array();
foo2[1] = new Array();

To call/define elements in a nested array, use two sets of square brackets.

foo2[0][0] = "something goes here";
foo2[0][1] = "something else";
foo2[1][0] = "another element";
foo2[1][1] = "yet another";
alert(foo2[0][0]);	// outputs "something goes here"

You can use shorthand notation with nested arrays, too.

var foo2 = [ ["something goes here", "something else"],
		["another element", "yet another"] ];

So that they're easier to read, you can spread these shorthand notations across multiple lines.

var foo2 = [ ["something goes here", "something else"], 
		["another element", "yet another"]

Properties and methods of the Array() objectEdit


The concat() method returns the combination of two or more arrays.

To use it, first you need two or more arrays to combine.

var array1 = ["a", "b", "c"];
var array2 = ["d", "e", "f"];

Then, make a third array and set its value to array1.concat(array2).

var array3 = array1.concat(array2);	// array3 now is: ["a", "b", "c", "d", "e", "f"]

join() and split()Edit

The join() method combines all the elements of an array into a single string, separated by a specified delimiter. If the delimiter is not specified, it is set to a comma. The split() is the opposite and splits up the contents of a string as elements of an array, based on a specified delimiter.

To use join(), first make an array.

var abc = ["a", "b", "c"];

Then, make a new variable and set it to abc.join().

var a = abc.join();	// "a, b, c"

You can also set a delimiter.

var b = abc.join("; ");	// "a; b; c"

To convert it back into an array with the String object's split() method.

var a2 = a.split(", ");	// ["a", "b", "c"]
var b2 = b.split("; ");	// ["a", "b", "c"]

pop() and shift()Edit

The pop() method removes and returns the last element of an array. The shift() method does the same with the first element. (Note: The shift() method also changes all the index numbers of the array. For example, array[0] is removed, array[1] becomes array[0], array[2] becomes array[1], and so on.)

First, make an array.

var array = ["0", "1", "2", "3"];

Then use pop() or shift().

alert(array);		// outputs "0, 1, 2, 3"
alert(array.pop());	// outputs "3"
alert(array);		// outputs "0, 1, 2"
alert(array.shift());	// outputs "0"
alert(array);		// outputs "1, 2"

push() and unshift()Edit

The push() and unshift() methods reverse the effect of pop() and shift(). The push() method adds an element to the end of an array and returns its new length. The unshift() method does the same with the beginning of the array (and like shift(), also adjusts the indexes of the elements.)

array.unshift("0");	// "0, 1, 2"
array.push("3");	// "0, 1, 2, 3"

Further readingEdit

Previous: Dates Index Next: Regular Expressions