Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Which built-in method removes the last element from an array and returns that element in JavaScript ?

  • Last Updated : 31 Dec, 2021

In this article, we will know how to remove the last element from an array using the built-in method in Javascript, along with understanding their implementation through the examples. There are a few Javascript built-in methods and techniques with which we can remove the last element of the array. We will focus only on the 2 methods ie., the pop() method & the slice() method.

JavaScript pop() Method: The pop() method is a built-in method that removes the last element from an array and returns that element. This method reduces the length of the array by 1.

Syntax:

array.pop(); 

Parameter: This method does not accept any parameter.

Return value: This method returns the removed element array, If the array is not empty, otherwise return undefined.

Example: This simple example illustrates the working of the pop() method.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1" />
    <title>
        Removing the last element from an array
    </title>
</head>
  
<body>
    <h1>GeekforGeeks</h1>
    <h3>Javascript pop() Method</h3>
  
    <script>
        let myArray = [2, 3, 5, 10, 11];
        console.log("The array is: ", myArray);
        const removedElement = myArray.pop();
        console.log("Removed Element is: ", removedElement);
        console.log("The array is: ", myArray);
    </script>
</body>
  
</html>

Explanation: In the first line, we have created an array and called the pop method to remove the last element, & on the next line where we have printed the returned value of the pop method, one element has been removed from the array. On the last line, the original array whose length is reduced by 1.

Output:

JavaScript splice() method: The splice() method is a built-in method that is used to manipulate the array by adding, deleting, or modifying the elements of the array in place. Here, we’ll discuss how we can delete the last element from the array with this method.

Syntax:

array.splice( index, remove_count, item_list );

Parameter values: 

  • index: It is a required parameter. This is an index where to start removing the elements in the array.
  • remove_count: The number of elements to remove from starting index.
  • items_list: The list of new items, separated by a comma, which is to be inserted from the starting index.

Return value: It returns an array, which may be empty or single-valued according to the number of elements removed.

Example: This example describes the splice method removing the last element from the array.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8" />
    <meta name="viewport" 
         content="width=device-width, 
                  initial-scale=1" />
    <title>Removing the last element from an array</title>
</head>
  
<body>
    <h1>GeekforGeeks</h1>
    <h3>Javascript splice() Method</h3>
    <script>
        let myArray = [2, 3, 5, 10, 11];
        console.log("The array is: ", myArray);
        const removedElement = myArray.splice(myArray.length - 1, 1);
        console.log("Removed Element is: ", removedElement);
        console.log("The array is: ", myArray);
    </script>
</body>
  
</html>

Explanation: In the first line, we have initialized an array and on the third line we have called the splice() method with myArray object, by providing the parameters as myArray.length-1 to start the deletion from the last index and 1 deleteCount to delete a single element. The removed element will be returned as a single-valued array. 

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!