Watching:

JavaScript Essential Training: Working With Arrays


show more An array is a single variable that holds multiple values. You create it quite similarly to creating a normal variable in JavaScript, but arrays always use square brackets. Learn how to create arrays in JavaScript, use their built-in methods (such as reverse, sort, and join), and find practical uses for arrays in JavaScript, such as identifying links or forms fields on a webpage. show less
please wait ...

Working with arrays

So we've seen how to create variables. We use the word var, we give it a name, and then we can use the equal sign to give it a value. That could be a number, that could be a string, that could be a Boolean. What we can also do is create arrays. An array is a single variable that holds multiple values. You create it quite similarly to creating a normal variable. I'm going to make one here called multipleValues. You can call it anything you'd like. But to tell JavaScript this is an array, there are a few ways to do it, but the easiest one is this: var multipleValues = opening square bracket, closing square bracket.

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.

I want the index at 1 to be equal to the value 60. I can say the index of 2 is equal to the value Mouse. And notice that what I can do is I can put in a number, I can put in a string, I could put in a Boolean; 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 through the index. That's whether you're setting these values, that's whether you're getting these values. And arrays in JavaScript use what's called a zero-based index. The first entry is at position 0, not position 1.

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.

And it will automatically put them in at position 0, position 1, and position 2. You could then come along and add on position 3, position 4, and so on, but it's always a zero-based index. Now, although this is my favorite way of saying to JavaScript, "Hey, I'm creating a variable that should be an array," there are actually multiple ways to write this, and there's multiple ways you'll see it when you're reading other people's code. An equivalent way is this: var multipleValues =. We use the word new and Array, Array with an uppercase A here.

Now for those of you coming from other languages, you might be familiar with this kind of format. That's because arrays are objects. They are a special kind of value that we're going to have a lot of dealings with shortly. And although this format saying = new Array will work, you don't have to even have the word new. You could just say this as well. All of these are exactly equivalent to each other. Now for those of you coming from other languages, you might be wondering what is the size of the array, how does JavaScript know how big this array is? Well, we can officially create it with a size/ In this case, I'm saying the array should have 5 slots, 0-4, but all arrays in JavaScript are dynamic.

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.

These are obviously very simple examples. We'll actually see some practical ones as we go forward. But the fact that arrays are objects just means we can ask them to do things, and very useful things in a lot of cases. If you're new to programming, you might be tempted to think of arrays as being esoteric things you're not really going to use all that much. Well, arrays are everywhere in JavaScript. You won't be able to get away from them. You may create some yourself, but a lot of them you'll be given. You'll have to deal with them.

For example, you might at some point want to write a little line of JavaScript that says, "I want to know how many anchor tags exist on the page, how many links do I have, or how many paragraphs do I have, or how many h3s do I have?" Well, what we can do to get that is called the document.getElementsByTagName method. And we'll get into this a little later. We haven't seen it yet. But this is a built-in ability of JavaScript to scan through our HTML, grab, in this case, everything that has an a tag, an anchor tag, and return it to us.

I could also ask for all the paragraphs, so all the p tags, or all the h1 tags, or h2s, or h3s. And what I'll get when I call this is an array. So whether there are four links on the page or a dozen or a hundred, I'll have an array to deal with. So whether you're asking, give me all the links that exist on the page, or you're wanting to go through all the form fields that exist on a web form, arrays are going to be your friends. You are going to have to deal with these. They are zero-based in JavaScript.

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.

Working with arrays
Video duration: 7m 57s 5h 31m Beginner

Viewers:

An array is a single variable that holds multiple values. You create it quite similarly to creating a normal variable in JavaScript, but arrays always use square brackets. Learn how to create arrays in JavaScript, use their built-in methods (such as reverse, sort, and join), and find practical uses for arrays in JavaScript, such as identifying links or forms fields on a webpage.

Subjects:
Developer Web
Software:
JavaScript
Author:
please wait ...