JavaScript Array pop() Method

JavaScript Array pop() Method | The last item of the array can be deleted using the array.pop() function, which also yields the eliminated item. By using this function, the array’s size is cut by 1.

The Array.prototype.pop() algorithm returns the eliminated element after removing the last element from an array. This tutorial will teach you how to fetch and delete the last element from an array using the JavaScript Array pop() method.

The syntax for the pop function is as follows:- array.pop()
There are no parameters accepted by the pop() array JavaScript method.

The deleted element in the array is returned by this method. The return value of this function is undefined if the array is empty.

Example-1: 
land_animals = [‘dog’, ‘cat’, ‘lion’, ‘fish’];
land_animals.pop();
Output:
The popped element will be “fish”.

Example 2:
numbers = [1,3,5,7,’hi’];
numbers.pop()
Output:
The popped element will be “hi”.

Let us look at some examples and situations in which the array.pop() function can be used.

JavaScript Array pop() Method Example

The four-member planets array is created using the code, and its final entry is subsequently eliminated.

Removing the end element from an array by using the JavaScript array pop() function

const planets = ['Mercury', 'Venus', 'Earth', 'Moon'];
const popped = planets.pop();

console.log(planets);
console.log(popped);

Output:-

[ ‘Mercury’, ‘Venus’, ‘Earth’ ]
Moon

The last entry of the array is eliminated using the pop() function in the example above. The “Moon” is eliminated from the “planets” array in this case using the pop() technique. Additionally, it displays which element was removed from the array.

JavaScript Array pop() Method Example with Empty Array

The pop() technique is used to refill a blank array in the instance below. The array’s size is zero in this situation, and the pop() method outputs undefined:

Applying the JavaScript array pop() method to an empty array

const planets = [];
const last = planets.pop();

console.log(last);
console.log(planets.length);

Output:-

undefined
0

JavaScript Array pop() Method Example with Array-like Objects

The basic pop() method is being used. Hence, to invoke the pop() procedure on the array-like object, utilize the call() or apply() functions. The last element to be removed is determined internally by the pop() function using the length attribute of the array-like object.

The “trees” object, which resembles an array with four components and a length variable, is created in the code below. After that, the last element is removed, and the length parameter is decreased.

JavaScript array-like objects and the pop() method

const trees = {
   0: 'banyan',
   1: 'mango',
   2: 'bonsai',
   3: 'palm',
   length: 4,
};

const popped = Array.prototype.pop.call(trees);
console.log(trees);
console.log(popped);

Output:-

{ ‘0’: ‘banyan’, ‘1’: ‘mango’, ‘2’: ‘bonsai’, length: 3 }
palm

Summary:

To fetch and get rid of the last member in an array in JavaScript, we can use the JavaScript pop() function. To run the pop() function on an object that resembles an array, use call() or apply().

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.