What Did The Software Developer Ask His Boss? If He Could Get Arrays. (The Answer Was No)

Jarrit Alicea
4 min readNov 20, 2020

Well if you couldn’t figure it out by the horrible pun in the title, today we’ll be learning the ins, and outs of arrays and array methods. JavaScript classifies arrays as objects but they’re most commonly known as just arrays. Arrays are precious tools that are implemented in all types of code. They are special variables that can hold a list of values (instead of just one) that can be specifically or entirely mutated (changed) at any point in time in your code.

To start off we need to declare a variable that will hold our array. It is important to understand that arrays are non-primitive data values. This means that they are mutable and can have their values changed at any time. Declaring an array using var, let, and const are all acceptable, but since only the value of the array will be changed and not it’s naming, it’s typically declared using const.

Want to read this story later? Save it in Journal.

Once you’ve declared a variable, you should initialize it to an empty array which is denoted by an opening square bracket and a closed square bracket `[]` just like in the example above. We call this empty because we haven’t actually put any values inside of it yet. You can think of these brackets as a box. If there’s nothing inside of the box, it is empty, but once something goes into it, then we know exactly what is inside of the box. This is where the list and specificity of arrays come in.

Every value that goes into an array has its very own index and every element is separated by a comma. If we go back to our “an array is a box” analogy then our index can be thought of as our list for the items in the box. Everything that goes into the box will be given a number that corresponds to its place inside the box. The index starts at 0 and can go on for however many items we put in the array. In the `colors` array above `red` is at the 0th index, `green` is at the 3rd index, and `violet` is all the way at the 6th index.

Now that we have a good understanding of arrays, we’ll start to talk about some basic array methods. Array methods are operations that can be used to mutate or traverse the array. Common methods are push, pop, shift, and unshift.

When you use an array method you are “calling” the method on the array. Calling the push method on an array adds a value to the end of the array and returns the new length of the array.

Calling the pop method on an array will take off the value that is at the end of the array and return it.

Calling the shift method is kind of like calling the pop method but at the beginning of the array. It removes the value at the first index and shifts all other values down one index while also returning the value that was shifted out.

Just like how the methods pop and shift are similar, so are the push and unshift methods. Unshift will add a new value to the beginning of the array and unshift all the index up while returning the new length of the array.

Now you know how to set up arrays, completely understand them, and even know some basic methods to perform on them. Coding is amazing and I wouldn’t want someone to get to a topic they don’t understand and just stop. Hopefully, this finds anyone that needs it and it helps them in any way possible. Have fun coding and never stop learning.

More from Journal

There are many Black creators doing incredible work in Tech. This collection of resources shines a light on some of us:

--

--