Accessing Array Elements in JavaScript

Beginning to learn how to code can be exciting! Sometimes it can be daunting, especially if you’re completely new to code. It requires plenty of practice, experimentation, dedication, and focus.

One important part of becoming a developer is knowing how to look for information in plain English to help you out. Here is a beginner resource you can use to figure out basic ways of accessing elements in an array. Feel free to code along with the examples. Let’s start!

Let’s begin by creating an array and assigning it to a variable:

This array made up of album names has four elements. Each element in an array has an index, a number for its place in the array. The first element in an array starts with the number 0. We can access the first element by using bracket notation.

Let’s put the following code in our console:

Since zero refers to the first element in our array we can see that it returns our first album:

How would we access the third element on our array? What number would we put in bracket notation? Let’s try this:

By pressing enter, what comes out?

It returned our third element in our album array, “1989”. Remember, array elements start out with an index of 0. So even though “1989” is in the third spot, we would put the number two because we counted from zero.

What if we had a nested array this time?

Now we can see that there are extra square brackets in our array. A nested array just means that we have an array inside of our original array. Though it may seem that “24k Magic” and “1989” are in their same places, we wouldn’t access them the same way. How many elements does our array have now? Let’s try this to find out:

Length is a property that we can add on to our array by using a dot. It checks to see how long an array is or, how many elements long an array is. As you can see our array of albums now has three elements.

Wait. Wasn’t and isn’t there four? Technically the fourth element is there, but it’s just nested. Let’s get the first element in our array:

And it returns:

Ok, cool. What about the next element? Let’s try it:

Ah, now we can see calling an index of 1 accesses the nested array. In albumArray[1], there are two elements:

How can we get the first element our nested array? Let’s take a look at our full array again:

We know that by doing albumArray[0], we get “Dirty Computer”. We also know that by doing albumArray[1], we get our nested array. If the same rules still apply in finding the first element in any array, then we can try using 0 to get the first element in our nested array. We’re going to do this:

Putting albumArray[1][0] gives us the first element in our nested array:

Let’s quickly break this down. If we did:

It would give us the array inside of our original array:

By adding an index of zero in square brackets to albumArray[1], we can access the first element of our nested array.

Nice.

I hope you found this beginner article helpful to access elements in an array. Please leave a clap below or share with other beginners!

Software Engineer, Artsy, Foodie, and Audiophile