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

Sorting

To sort elements, use the sort() function.

var groceryList = ["Tomatoes", "Lemons", "Apples", "Bananas"];
groceryList.sort();
// ["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?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Build modern and responsive webpages

Responsive Typography

Build modern and responsive webpages Try Front-end

Responsive web design helps your site maintain its design integrity on a variety of screen sizes, but how does it affect your typography? With this practical book, graphic designers, web designers, and front-end developers alike will learn the nuts and bolts of how to get the best appearance from type without sacrificing performance on any device.

$ Check price
24.9924.99Amazon 4.5 logo(3+ reviews)

More Front-end resources

Ad