Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
An array is a collection of values all wrapped up and given a name. Okay, so what does that mean? Well, we already know how to create one variable at a time. We use the word var, we give it a name, and then we can use the equal sign to give that variable a value. That could be a letter, it could be a number, it could be a string, it could be a Boolean, but it's one value. Now an array is the idea of multiple values but all contained in one named variable such as this one.
We have a variable called myArray with multiple values in it. It's a great way to keep data together that belongs together, to keep dozens, hundreds, or even thousands of pieces of information together without having to name every single individual piece as an individual variable. But before we make one, here are a couple of concepts. See if all we have is one name for this entire array, how do we get individual pieces in or out? Well in an array each individual value, often referred to as an element, has a number, an index that identifies where it is in the array.
Well how do you do that, how do I get to the individual elements of that array? I use these square brackets again. So if I want this array to hold multiple values we need to be able to say which element of the array are we trying to get to? So we use the index. So here is an example. I use the name of the variable but instead of just saying equals, I am using those square brackets again. I'm saying here I want the element at position 0 to be set to the value 50 and I want the element at position 1 to be set to the value 60.
I can say I want the element of position 2 to be able to the word "Hello" and notice that what I can do in this array is I can put in a number, I can put in a string, I could also put in a Boolean, I can actually put anything in there. It doesn't matter what kind of data you're putting in at the different slots in the array, but all of them are accessed using that index and that's whether you are setting these values or whether you're getting these values. It's always a 0-based index.
Now what we can then do is use the same format to get to the contents of the array. So for example, if I want to write an alert message with whatever is at the second position I use the square brackets. I use the same way to access them and to set them and it writes out the word "Hello" in this case. So if this is the way that we write arrays. It's okay, it's not too bad, but it would be nice if there was something a little quicker, and in fact there is. There is a shorthand method for doing it.
So instead of doing it over several statements like this, we can use those square brackets and combine all this stuff onto one line. We can just load this array up with the initial values. In this case, 50, 60, "Hello", and it will automatically create an array and put them in that position 0, position 1, position 2. You could then come along and add position 3, position 4, and so on but it's always a 0-based index. Now arrays are found in every language, and as we will see there are other ways of grouping values together.
Sometimes you might not want a 0-based index but perhaps a letter-based index or some other way of accessing the elements of the array. And we can do that too. But these arrays are the classic way to get started with what we are often referred to as collections in a programming language, and this time the buzz word really is straightforward. A collection in a programming language is simply multiple values grouped together in some way, and there are often slightly different ways of creating them across languages but the overall concept is the same.
Get unlimited access to all courses for just $25/month.Become a member