Adding an element to the end of a JavaScript array

JavaScript for Beginners - Arrays: Adding and Removing Elements Part I - Working with the ends of a JavaScript array
3 minutes
Share the link to this page
Copied
  Completed

Transcript

In this class, you'll learn how to add elements to or remove elements from a JavaScript array. This class is divided into two parts. Part One focuses with the ends of the array, specifically, the very first or the very last elements of the array, you'll learn how to add an element to the end, remove the last element, add an element to the beginning or remove the very first element, part two will focus on the middle of the array. So let's dive into part one, working with the ends of an array, and I mean, specifically, the very first or the very last elements of the array, adding an element to the end of an array. The push method provides a very simple way to add one element to the end of a JavaScript array. For example, here I have this array foo, and there's three elements A, B, and C. And let's say I'd like to add the letter D to the end of this array.

So I would do that by calling the push method I would say food up, push, D, and when I do that, De winds up at the end of the array. But what is this? Why do I have the number four there in the middle of the screen? Well, that's because the push method always returns the new length of the array. So if I were to execute this push method again, and maybe add the letter E to the end of the array, I'd wind up adding literally to the end of the array, but the push method will return the number five, because the push method always returns the length of the new array. Let's look at a code example for this in JavaScript, the push method as one element to the end of an array.

I'm going to take this example code and paste it here in the browser. I don't need the comments. And for now, we simply want to define the array. And then I'll add one to do So first, let's define the array. So now when I inspect We can see it has four elements. So now use the push method, that one element to the end of the array, and it returns the new length of the array.

So calling the method actually returns the number four, because that's the new length of the array. If I type foo. length, I get four. Now when I inspect foo, I see it's got four elements. So let's say that I wanted to add another element to the end of the array. So I'll call the push method again.

And notice again, now it has four elements now. And now I'll use a push method, add one more element, and this will return the new length of the array, which will be five. Now the length is five. If I do foo dot length, I get five and when I inspect foo, we can see that it has five elements. So I can do one more time I'm going to use the push method to add one more element to the end. Food.

So now we should have the letter F at the end of the array of the last element and the new length is six, and wines when I inspect food, we see that it has six elements. So the push method will add whatever you pass to it to the end of an array, it will always make it the last element in the array and overturn the new length of the array.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.