https://blog.kevinchisholm.com
In this video, I'll explain how to remove the last element of a JavaScript Array.
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 the letter "f" and it will return that element that it removed, the letter "f", and the length will change from 6 to 5.
Let's see this in action. So first, I'll want to define the array. So I've got this array called "foo" 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 6 ... it's an array with 6 elements, the last one is "f".[a] 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 6 to 5. So, here, it returns the letter "f", and then if I do "foo.length", we see the length of the array is now 5. If I inspect "foo", you can see it no longer has the letter "f". It's just "a", "b", "c", "d", "e".
So now, if I call "foo.pop" again, it should return the letter "e" and it should change the length from 5 to 4. 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 4 elements in the array. 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 the array will change.
If you found this video helpful, you can subscribe to my channel by clicking the red "Subscribe" button right under the video. You can also take a look at my blog, where there are many articles and tutorials about web development.
Thanks very much for watching.