05. Arrays

An array is an object that stores data that can be accessed with a numerical index value. Just like most programming languages, JavaScript starts its array indices start at 0. However, unlike some programming languages, JavaScript does not require the data within each array slot to be of the same type.

Declaring arrays

There are a few ways to declare an array. The following code shows three ways to create the same exact array.

// Insert one item at a time
var groceryList = new Array();
groceryList[0] = "Bananas";
groceryList[1] = "Pasta";
groceryList[2] = "Pumpkin";
groceryList[3] = "Tomato sauce";
// Place an array within a variable
var groceryList = ["Bananas", "Pasta", "Pumpkin", "Tomato sauce"];
// Call the array constructor with arguments
var groceryList = new Array("Bananas", "Pasta", "Pumpkin", "Tomato sauce");

Declaring an array of a set size

If you already know what the size of an array ought to be, you can do so upon initialization.

var myArray = new Array(10);

Retrieving array values

To retrieve an array value, simply use brackets ([]), with the index number placed inside.

To access the array items in a more object-oriented manner, you may use the dot notation, with the item(0) method.

groceryList[0];      // Bananas
groceryList.item(0); // Bananas

Changing array values

Changing an array value is the same method as assigning. The following code change the 3rd item in the grocery array to apples.

groceryList[2] = 'apples';

Adding and removing elements

There are two sets of functions used to add and remove elements, depending on whether you want to add to the front of back.

Adding to the front - shift and unshift

To add or remove elements to the front, use the shift() and unshift() functions.

var groceryList = ["Bananas", "Pasta", "Pumpkin", "Tomato sauce"];
groceryList.unshift("Chicken breast");
// ["Chicken breast", "Bananas", "Pasta", "Pumpkin", "Tomato sauce"]
groceryList.shift(); // now remove the front
// ["Bananas", "Pasta", "Pumpkin", "Tomato sauce"]

Adding to the back - push and pop

To add or remove elements from the back, use the push() and pop() functions.

var groceryList = ["Bananas", "Pasta", "Pumpkin", "Tomato sauce"];
groceryList.push("Chicken breast");
// ["Bananas", "Pasta", "Pumpkin", "Tomato sauce", "Chicken breast"]
groceryList.pop(); // now remove the front
// ["Bananas", "Pasta", "Pumpkin", "Tomato sauce"]


To sort elements, use the sort() function.

var groceryList = ["Tomatoes", "Lemons", "Apples", "Bananas"];
// ["Apples", "Bananas", "Lemons", "Tomatoes"]

Array attributes

To access the length of an array, access the length property.

var numGroceryItems = groceryList.length;  // 4

Concatenating two arrays

To add two arrays together, use the .concat function.

var arrayOne = [1, 2, 3];
var arrayTwo = [4, 5, 6];
var concatArray = arrayOne.concat(arrayTwo); // [1, 2, 3, 4, 5, 6]

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Build modern and responsive webpages

JavaScript & JQuery

Build modern and responsive webpages Try Front-end

This book was written for anyone who wants to use JavaScript to make their websites a little more interesting, engaging, interactive, or usable. In particular, it is aimed at people who do not have a degree in computer science (well, not yet anyway).

$ Check price
39.9939.99Amazon 4.5 logo(385+ reviews)

More Front-end resources