Intro to Arrays

A beginners introduction to arrays in JavaScript

Kevin Glasgow
4 min readMay 29, 2021

What is an array?

An array, according to Merriam-Webster definition #2, is `to set or place in order’(a) or `to set or set forth in order (something, such as a jury)’(b). For being such a general definition of an array these two get us awfully close to understanding what an array is in JavaScript. A more technical definition is that an array is a list style object where multiple values can be stored within a single variable. Those values can be any type of data such as a string, a number, or a Boolean.

An array is not limited to only storing primitive data types and has the ability to store multiple objects and even arrays within itself. This is more of an advanced concept, but if you are intrigued after this crash course, I empower you to simply google array of arrays and explore.

But how do we make one and what does it look like?

While it is very possible that you have already seen or even worked with an array in your JavaScript code, but for everyone sake, we will be starting at square one. Now that we know what an array is and that it can be used to hold one, but in most cases, more than one type of data. So, let us use that example and create an array for a jury.

Simple as that. We have declared our variable of jury that is an empty array. Let us fill in our jurors because this would be a real quick case dismissed without the jury. For this example, we will be using the classic 12 Angry men.

That is a little long and for longer arrays I prefer to do this for better readability,

We have now added our jurors to our array of jury. You will notice that each juror’s name is represented as a string, each string except the last is followed by a comma as well as a space. This is the correct way to declare different objects within your array. A quick console log will simply return that array.

Now you might be thinking great we have our array of 12 angry jurors, I want to know who the last juror is. So, to do this you are probably thinking we simply need to console log our juror at position 12 in our array like so.

Well I’m sorry to be the bearer of bad news but…

Why did this happen?

Well, this is because arrays positions run off an index, and hint hint, our index does not start at 1. It starts at 0. Think of a hotel if you will, technically when you walk in and go to the check-in desk you would be on the first floor. This is not the case, you are technically on the ground floor, zero if you will. You have gotten your key for room 105, which happens to be up the stairs one floor, to the first floor. The first floor just happens to be on the second story, and the same holds true with arrays.

Lets take a look at this example where we will try and grab our first juror,

Returns

Awesome, we were able to grab him because we console logged our jury at the ground floor, zero. If you have already done the math, and we still wanted to know who our twelfth juror is. We would need to index our array of jury at 11,

Returns

I would like to thank you for reading along and hope that you have learned something new! Keep an eye out for more articles in the future!

--

--

Kevin Glasgow
Kevin Glasgow

Written by Kevin Glasgow

Full Stack Software Engineer | Passion for Frontend and Design | Dirt Rider & Snow Glider | www.linkedin.com/in/kevin-glasgow/

Responses (1)