Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Array pop() Method

JavaScript Array pop() Method

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




function func() {
    let arr = ['GFG', 'gfg', 'g4g', 'neveropen'];
 
    // Popping the last element from the array
    console.log(arr.pop());
}
func();


Output:

neveropen

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

JavaScript




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


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




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

RELATED ARTICLES

Most Popular

Recent Comments