Pop
The pop
method removes the last element from an array and returns that element. This method changes the length of the array.
Here's the syntax for using pop
:
array.pop();
For example:
let arr = ["one", "two", "three", "four", "five"];
arr.pop();
console.log(arr);
// Result: ['one', 'two', 'three', 'four']
You can also use the pop
method in conjunction with a loop to remove all elements from an array. Here's an example of how you might do this:
while (array.length > 0) {
array.pop();
}
console.log(array); // Result: []
The pop
method only works on arrays, and not on other objects that are similar to arrays such as arguments objects or NodeList objects. If you need to pop elements from one of these types of objects, you will need to convert it to an array first using the Array.prototype.slice()
method.