Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
And this is the indicator that we're dealing with an array. When you see the square brackets, it's a pretty good indicator you've got an array. This line simply creates a single variable called multipleValues that you can put several things inside. Well, how do you do that? Using the square brackets again. If we want this array to hold multiple values, we need to be able to say which one are we dealing with, so we use an index. And I'm saying here I want the index at 0 to be equal to the value 50.
And that's very common with most languages. What we can then do is use the same format to get to the contents of the array. So if I want to write out a console.log message, I want to get the value that's at position 2 in the array called multipleValues, I use the square brackets and it writes out the word Mouse. 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. We use the square brackets and we just load it up with those initial values, in this case, 50, 60, Mouse, inside double quotes.
They can be as big as you want them to be. If you want something to exist at position 5, you simply load it into position 5. If you want it to exist at position 100, you load it into position 100. While there is a theoretical fixed limit to the size that an array can get to, you're never going to run into to it. So my favorite way is just the square brackets by themselves. I think that's the most straightforward way of doing it. Now because arrays are objects, they have properties, and properties means information that we can get to about them.
So if I write a line of code that creates an array and loads it up with some initial values, as in this case, what I can actually do is use the name of the array-- in this case multipleValues--then a dot, and then I can access the length property-- it's information about the array. So in this case, I can write out a message that says the length of this array is 5. We have five elements in it. Now, the highest index might be 4. We're going to 0, 1, 2, 3, 4, but there are five elements in there. The length is 5.
And because arrays are objects, they also have methods. Now what does that mean? Well, we know how to call a function. We use the name of the function, we use the parentheses, and optionally pass in any parameters that function requires. Well, a method is a function that belongs to an object. So if we've created an array or created another kind of object, as we'll see shortly, we can use the name of that object, then a dot, then the name of the method to call a method. Here's an example.
So I create an array with five values in it. I've got this object and it's called multipleValues. What I can then do is say the name of the object, dot, and then the name of the method. It's like a function that belongs to it. Well, arrays have built-in methods. We've got reverse as one of them. In fact, when I call reverse on this array, what it's going to do is give me another array, and I can use the result of that to actually create a new variable, in this case var reversedValues, which is also an array.
So.reverse() is a method of the array object, as is.join(), which would join two together, as is.sort(), and I call them all using this dot operator. But by calling.reverse(), I have this new reversedValues array, and I could actually call the methods of it. So I could write out console.log ( reversedValues.join() ) which is a method of array, and that will actually join all the values in the array together and output them as a single string, in this case, writing the contents of it, which are the reversed values, back out.
They are dynamic, meaning that you can change them very easily. You don't have to use special code to do that. You're always going to see the square brackets to deal with them. That's always that identifier to you that you're dealing with an array.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105127 Viewers
56 Video lessons · 116901 Viewers
71 Video lessons · 86115 Viewers
131 Video lessons · 41180 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.