Removing the last element in the end of a JavaScript array. The JavaScript pop method provides a very simple way to remove the last element of the array. So once again, looking at our foo array, we have the elements A, B, and C. So what if I wanted to remove c? Well, that's the last element. So if I just want to remove the last element of the array, I can use the pop method to remove C, which is the last element. So when I do that, if I say food up, pop, C is removed from the array, but then I had the literacy here in the middle of the screen.
So what is this? Why is the literacy just sitting there? And that's because the pop method always returns the element that was removed from the array. So when I said food pop, it removed the last element which was C, but it returned C. So if I would execute that the pop method again, what would happen is, I would remove B from the end of the array, but it would actually the method would actually return the letter. Be. So let's look at a code example for that one.
In JavaScript, the pop method removes the last element of the array and returns that element. So here I've got an array called foo. And if I call the pop method, it should remove the last element, which is a letter F. And it will return that element that it removes the letter F, and the length will change from six to five. Let's see this in action. So first, I want to define the array. So I've got this array called foo now and if I inspect, foo, we'll see it's got an element with six.
It's an array with six elements. The last one is F. So now, if I call the pop method of foo, it should do two things, it should return the letter F, which we'll see in a second and the length of foo should change from six to five. So here it returned the letter F. And then if I do foo dot length, see the length of the array is now five. If I inspect food, You can see it no longer has a letter F. It's just ABCDE. So now if I call food pop again, it should return the letter E. And it should change the length from five to four. So execute pop, it returns the letter E. And then if I want to look at foo again, I can see that it's got four elements in it right.
And then if I execute, pop one more time, it should return the letter D, which is the last element of the array, and then foo should now have three elements in it. So the pop method always returns the last element of the array. It removes the last element and returns it. And of course, the length of that array will change