Skip to content
Related Articles

Related Articles

JavaScript Array pop() Method

Improve Article
Save Article
  • Last Updated : 30 Nov, 2022
Improve Article
Save Article

The 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.

 Syntax:

arr.pop()

Parameters: This method does not accept any parameter.

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

Below is an example of the Array pop() method.

Example 1: 

JavaScript




<script>
    function func() {
        var arr = ['GFG', 'gfg', 'g4g', 'GeeksforGeeks'];
      
        // Popping the last element from the array
        console.log(arr.pop());
    }
    func();
</script>

Output:

GeeksforGeeks

Example 2: In this example, the pop() method removes the last element from the array, which is 4, and returns it.

JavaScript




<script>
function func() {
    var arr = [34, 234, 567, 4];
  
    // Popping the last element from the array
    var popped = arr.pop();
    console.log(popped);
    console.log(arr);
}
func();
</script>

Output:

4
34,234,567

Example 3: In this example, 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




<script>
function func() {
    var arr = [];
  
    // popping the last element
    var popped = arr.pop();
    console.log(popped);
}
func();
</script>

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: The browsers supported by the JavaScript Array pop() method are listed below:

  • Google Chrome 1.0 and above
  • Microsoft Edge 12 and above
  • Mozilla Firefox 1.0 and above
  • Safari 1 and above
  • Opera 4 and above

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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!