Using  the Pop()  Method to Manipulate an  Array  In JavaScript

Using the Pop() Method to Manipulate an Array In JavaScript

In my last article, I explained how to use the pop() method to manipulate an array in JavaScript.

In this article, I will be explaining how to use the Pop() method to remove the last element in an array.

Pop() Method In JavaScript

The pop () method removes the last element of an array and returns that item. It always changes the length of an array ( the original array). It is also used to return the removed elements. The pop () method relies on the length property and returns undefined when the array is empty.

Steps to pop() (remove) the last element in an array include:

Open the console tab

  1. Write a list of items in an array in the opened console tab. Remember to put them in strings. For example,

    let food = ["bread", "rice", "beans", "yam", "spaghetti"];

  2. Remove the last element in the list of the array above by using the food.pop(), and press the enter key. When you press the enter key the last element is removed and the new length and list would be displayed. Remember the index started from 0 from the left to the right side.

    new index = 3

    ["bread", "rice", "beans", "yam"];

    As you can see, the last element "spaghetti" has been removed from the list and the new list has been displayed.