Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptWhich built-in method removes the last element from an array and returns...

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

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. 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments