Recently I was asked to make a 5 minute video teaching someone about javascript arrays and objects. They wanted the material presented to a person who just started to learn how to code. I started to think about what I wanted to present in just 5 minutes. I came to the realization that there is SO much more to javascript arrays when you are starting out as a beginner than you realize after using them for awhile. I decided to start with the very basics.
An array in javascript is a numbered list or a collection of items. It can be very beneficial to have when it comes time to iterate through all of your values compared to creating an individual variable for each value you have.
Using array literals is one of the easiest ways to create a new array. That looks like this:
let variableName = [ "value1", "value2", "value3" ]
Another way to create a new array is by using the javascript keyword Array.
let variableName = new Array( "value1", "value2", "value3")
I think it's important to note that while I was using strings to create the values of my arrays you also have the option to use pretty many any of the other datatypes.
The next step after learning how to create an array is to learn how to access the different values in that array. This leads to another important note for beginners. You might look at the array list and assume that "value1" is going to be the first index on the list, however programmers like to start counting with zero. So it's important to know that every list item you see matches the index that is one less than its place on the list. Now that we have that out of the way here is how you access those values.
Let's say we want to access the 2nd index of our array we made earlier. We would write it like this:
variableName[2]
This would return back to use "value3", as that is our current value as the 2nd index (or 3rd item in our list).
Those are some of the very basics to javascript arrays. If you'd like to learn more, stay tuned for my next blog. Where I'll be going over some of the properties of arrays and shifting around indexes of an array.
Thanks for reading!
Top comments (0)