JavaScript/Arrays

Previous: Dates Index Next: Operators

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"];

ExerciseEdit

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

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

concat()Edit

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

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

var arr1 = ["a","b","c"];
var arr2 = ["d","e","f"];

Then, make a third array and set its value to arr1.concat(arr2).

var arr3 = arr1.concat(arr2) //arr3 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, arr[0] is removed, arr[1] becomes arr[0], arr[2] becomes arr[1], and so on.)

First, make an array.

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

Then use pop() or shift().

alert(arr); //outputs "0,1,2,3"
alert(arr.pop()); //outputs "3"
alert(arr); //outputs "0,1,2"
alert(arr.shift()); //outputs "0"
alert(arr); //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.)

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

Further readingEdit


Previous: Dates Index Next: Operators
Last modified on 13 November 2013, at 05:01