Arrays

Contact Us or call 1-877-932-8228
Arrays

Arrays

Arrays in JavaScript are, of course, collections of values with numeric, zero-based indices. We can create an array variable by assigning it an array literal:

var a = ['apple', 'banana', 'cherry'];

or by explicitly instantiating a new Array object:

var a = new Array('apple', 'banana', 'cherry');

and get the same result. We access elements of the array with the square-bracket operator, both to get and to set values:

document.write(a[0]);
a[1] = 'orange';

We can store anything - different anythings - in the same array: the first element of any array might contain a string, the second an integer, the third an object, the fourth a function, etc. The length property gives us the current size of an array:

document.write(a.length);

Following this brief review of arrays, we will in this lesson take a look at some perhaps less well-known, more recent, and useful methods from the built-in JavaScript Array class.

Next