Arrays

This YouTube video was created by Steve Griffith.

This Scrimba screencast was created by Dylan C. Israel.

In most programming languages, it is possible to store list of values into a single container. In JavaScript, this container is known as an array. An array is a comma separated list of values, enclosed by a set of square brackets.

Creating an Array

In most programming languages there are arrays for groups of items.

An example of an array could be a grocery list. The array opens and closes with square bracket notation [] and each item inside is separated by a comma.

Accessing Array Items

Our list of groceries are grouped in an array that we assign to the variable groceries.

In this list every item in the array is assigned an order starting from 0 - Up. 0 will always be the first item in the array.

The first item in the groceries array is "Apples". "Apples" index is 0. To print out a specific item in the array we can refer to the array with the index of the item.

Accessing the Last Item of the Array

If our list is growing and we are unsure of the total size of the Array we can check the length of the array to determine the last item's index. In this example we see the length of our groceries array is 4. Yogurt is the last item in the array at index 3. By subtracting 1 from the length of the array we determine the position of the last item. "Yogurt".

Sorting Arrays

We can also rearrange our arrays alphabetically.

We can then reverse the array order to sort it from Z-A.

Working with numerical values does not sorting numerically, but alphabetically.

We can then reverse the order from highest to lowest.

Adding Items to an Array

To add an item to the end of our array we can use Array.push(item). In this example we add "Olives" to the end of our groceries array.

To add an item at the beginning of our array we can use Array.unshift(item). In this example we are adding "Olives" to the top of our groceries array.

To add an item somewhere in between we need to use Array.splice(pos, item) to push an item into a specific position in the array. In this example we push "Olives" into the second position of the groceries array.

If we have two arrays we can also combine those together. If there was another list of groceries we initially forgot to include we can concatenate these arrays together for a complete grocery list.

Removing Items from an Array

To remove the last item of the array we can use Array.pop(). The last item "Yogurt" was removed from the groceries array.

To remove the first item in the array we can use Array.shift(). The first item "Apples" was removed from the groceries array.

To remove a specific item in the Array we use the Array.splice method. In this example we are removing the 3rd item from the groceries array. Unfortunately, we've removed "Ice Cream". The third Item occupies the second index. [0][1][2]

Looking up items in array There are many cases in which we might not know the position of the item in the array but we know what we are looking for. Let's take a look at how we would remove the value "Milk" from this array. First we will search for the value "Milk" in the array, this will return the position "Milk" is found.