Which built-in method removes the last element from an array and returns that element in JavaScript ?
Last Updated :
09 Jan, 2023
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 its 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 returns undefined.
Example: This simple example illustrates the working of the pop() method.
JavaScript
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);
Output:
The array is: (5) [2, 3, 5, 10, 11]
Removed Element is: 11
The array is: (4) [2, 3, 5, 10]
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.
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.
JavaScript
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);
Output:
The array is: (5) [2, 3, 5, 10, 11]
Removed Element is: [11]
The array is: (4) [2, 3, 5, 10]
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.
Similar Reads
Which one method use to delete an element from JavaScript array (slice or delete) ? We can use both methods to delete an element from a javascript array. Â The answer is solely dependent on the application and on the programmer. What slice() method does? The slice method, as the name suggests, slices an array and returns a copy of the new array. This method does not affect the main
4 min read
How to remove specific elements from the left of a given array of elements using JavaScript ? In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met
2 min read
How to get removed elements of a given array until the passed function returns true in JavaScript ? The arrays in JavaScript have many methods which make many operations easy. In this article let us see different ways how to get the removed elements before the passed function returns something. Let us take a sorted array and the task is to remove all the elements less than the limiter value passed
4 min read
How to Remove First and Last Element from Array using JavaScript? Removing the first and last elements from an array is a common operation. This can be useful in various scenarios, such as data processing, filtering, or manipulation. Example:Input: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]Output: [ 2, 3, 4, 5, 6, 7, 8, 9 ]Removing first and last element in array can be do
2 min read
Delete the first element of array without using shift() method in JavaScript Given an array containing some array elements and the task is to remove the first element from the array and thus reduce the size by 1. We are going to perform shift() method operation without actually using it with the help of JavaScript. There are two approaches that are discussed below: Table of
2 min read
How to delete an element from an array using JavaScript ? The array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index. In this article, we will discuss different ways to remove elements from the array.
5 min read