Adding and Removing Array Elements

Modern JavaScript Working with Arrays
7 minutes
Share the link to this page
Copied
  Completed

Transcript

In the previous movie, all of the values that the array contained were added at the time the array was defined. However, arrays are dynamic. And you can add and remove elements, the array or values from the ray at any time. So let's open up the console and take a look at the methods that are used to add and remove array elements. Make this just a bit larger. All right, let's first define an array.

This is going to be an empty array. So right now there is nothing in it. We know that from the length property shows zero. All right, let me clear what we have here. Now let's start adding some lm To this array using some different techniques. So first I'm going to specify the index where I want to add this to, I'm going to add a 50 to position one in the array.

Now remember arrays start with an index of zero. So this is actually the second position. I press return. It seems to have except that let's display it now. Sure enough, it's there. What is in position zero.

Position zero is undefined. And if we do the length is showing that there are two values within this array. It's just one of them is undefined. So when we added something to the second position, index one, since there wasn't something already in the first position index zero Yeah. automatically set that to undefined so that it could add a value to the second position. All right, so you can add elements to an array using the brackets.

So I can now add something to zero. And now zero has a value. All right, that's one way to add to an array. Another way is using the push method. So let's say we wanted to add a 79. One more score to this array, we have a 100 and a 50.

Right now, let's add a 79. We simply push 79. And what that does, a push causes the 79 to be added at the end of the array. So if we were to do the last position That's where the 79 is. Now with the push method, you can also do more than one number at a time. So we could do a 55 and a 67. at both of those, now returns a five indicating the length of our array is five, and it's added those two numbers at the end.

So if we were to do if we were to check the fourth position, that would be a 67. I should say if we were to check the fourth index, which is the fifth position, that would be a 67. Now, as I indicated, the push command adds a value at the end of the array. Using the push command is just like using this inside of the brackets, I'm going to do array dot length. So the length is now five. So it's going to ask Add a new element at index five.

Now we can check that. And sure enough, that's where it's at. Now, if you want to add an element at the beginning of an array, you would use the unshifted method. This works exactly like push, except the elements added at the start. So now if we do array zero, that's where 58 is. And the 100 that used to be there should be at position one now.

Now what if we wanted to move remove elements from an array? Well, there's a couple of ways to do that as well. If you want to remove something from the end of the array you use Part method 97. Now let's see what our length is. It's now down to six. So when you use pop, it removes that from the array, it just doesn't return the value.

If you just want to reference the value, you do that using the brackets and then indicate the index of the value you want to retrieve, pop actually returns the value and removes it from the array. So for example, if we wanted to place that into a variable, we would use this statement similar to this. So now, lm has a value 67. And now array dot length is now down to five because we doesn't pop twice, it's gone from seven to five. Now pop as I said, removes it from the end of the rate if you want to remove a value from the beginning of the of the array, you use the shift method. So that removed from the beginning of the array, let's check our our length again.

And now it's down to four. So shift works just like pop, except it takes it from the beginning of the array as opposed to the end of the array. So there we looked at four methods for adding and removing elements from an array. Alright, let's review those four methods before we add this movie. Pop push pop shift and unshifted push will add value to the end of an array on shift will add value to this Part of an array, shift will remove the first value in an array and pop will remove the last value in an array. And it doesn't just return the value it completely removes it.

Alright, let's move on to the next topic.

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.