Data structures are formats that we can use to keep track of our data in an organized fashion.


  • one very basic data structure
  • Programmers use lists as a container to store multiple pieces of information that relate to each other in some way. 
  • orders data in a specific, linear sequence

Access Items in a List

  • order items so that they’re in a specific sequence.

Index (or indices – plural) is the position of value in a list. It’s like an address that we use to locate an item in a list. It’s a number that starts at 0.

Add Items to a List

Append to the end of the list – add things to the end of an existing list

myList = ['apple', 'banana', 'pear']
// now, myList == ['apple', 'banana', 'pear', 'orange']

Add item to an existing list, with index number for where we want to position our new value.

myList = ['apple', 'banana', 'pear']
myList.splice(1, 0, 'mango') // make 'mango' the second item in the list
// now, myList = ['apple', 'mango', 'banana', 'pear']

In JavaScript, we can edit lists with commands like .splice and .pop.

Removing Items from a List

Remove last item.

myList = ['apple', 'banana', 'pear']
// removes 'pear' from myList and returns 'pear'
// now, myList == ['apple', 'banana']

Remove items from the middle:

myList = ['apple', 'banana', 'pear']
myList.splice(1, 1) // removes 'banana' from myList and returns 'banana'
// now, myList == ['apple', 'pear']

In JavaScript, lists are created by defining a name for the list (such as myList), then setting it equal to a set of brackets []. If there’s nothing in between the brackets, the list is empty. But myList has three items in between brackets [], each separated by a comma ,:

myList = ['apple', 'banana', 'pear']

You may have noticed that those were all strings. We can also put other data types in a list, including numbers and boolean values. We can even put other lists in a list!

To select an item:

myList = [0]

I am selecting the first item from the list or 'apple'
colors = ['red', 'yellow', 'green', 'blue']

To select the third item in the list, we would write colors[2]:

// the following line will return 'green'

We could also save this selection to a variable, so we can use it later in our code:

// now myFavoriteColor is equal to 'green'
myFavoriteColor = colors[2]
// Build a list
comicStrip = ['Codey sees the trail', 'Codey starts the hike', 'Codey is halfway', 'Codey reaches the finish']

// select the 4th item from the list
selection = comicStrip[3]
// and save it to the variable selection

The larger the sample size and the more diverse your dataset is, the more confident you’ll be in your results.

Leave a Reply

Your email address will not be published.