JavaScript Array pop() Method

Below is the example of Array pop() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  • Output:
    GeeksforGeeks

The arr.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 by 1.

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 examples illustrate the JavaScript Array pop() method:

  • Example 1: In this example the pop() method removes the last element from the array, which is 4 and returns it.
    var arr = [34, 234, 567, 4];
    var popped = arr.pop();
    print(popped);
    print(arr);
    

    Output:

    4
    34,234,567
    
  • Example 2: 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.
    var arr = [];
    var popped = arr.pop();
    print(popped);
    

    Output:

    undefined

More example codes for the above method are as follows:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

4
34,234,567

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

undefined

Supported Browsers: The browsers supported by JavaScript Array pop() method are listed below:

  • Google Chrome 1.0
  • Microsoft Edge 5.5
  • Mozilla Firefox 1.0
  • Safari
  • Opera

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.