Open In App

JavaScript Array pop() Method

JavaScript Array pop() Method is used to remove the last element of the array and also returns the removed element. This function decreases the length of the array.

 Array pop() Method Syntax

arr.pop();

Array pop() Method Return value

This method returns the removed element array. If the array is empty, then this function returns undefined.

Array pop() Method Examples

Example 1: Removing the Last Element from the Array

The pop() method in JavaScript removes the last element from an array and returns that element. In this example, it removes and prints the last element 'GeeksforGeeks' from the array ['GFG', 'gfg', 'g4g', 'GeeksforGeeks'].

JavaScript
function func() {
    let arr = ['GFG', 'gfg', 'g4g', 'GeeksforGeeks'];

    // Popping the last element from the array
    console.log(arr.pop());
}
func();

Output:

GeeksforGeeks

Example 2: Removing element from empty array

Here, the function pop() tries to extract the last element of the array but since the array is empty therefore it returns undefined as the answer.

JavaScript
function func() {
    let arr = [];

    // popping the last element
    let popped = arr.pop();
    console.log(popped);
}
func();

Output:

undefined

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers:

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

Article Tags :