Array.prototype.pop() by JavaScript convention.

let fruit = ['apple', 'orange', 'banana', 'tomato'];
let popped = fruit.pop();

console.log(popped); // "tomato"
console.log(fruit); // ["apple", "orange", "banana"]
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Stuart Kershaw on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @iamdarius โ€บ 4-ways-to-remove-the-last-element-from-an-array-in-javascript-17749b12be0c
Learn 4 Ways to Remove the Last Element from an Array in JavaScript | by Darius Moore | Medium
September 8, 2022 - Splice is a powerful method that can be used to add new elements to an array, remove elements from an array, or perform both actions simultaneously. Unlike slice, splice mutates the original array that it is being invoked on. Below, the start count (the first parameter) is given an argument of -1, and the delete count (the second parameter), is 1. These two arguments indicate that the delete operation will begin at the last element of the array, and will only delete one item, respectively.
Discussions

Is there an easy and elegant way of removing the last element of an array?
Arraylist has been deprecated for a good long while now. Use a [system.collections,generic.list[]] instead. They can be strongly typed and provide a mechanism to control integrity and better error handling when doing so. Plus they donโ€™t have output when you manipulate them unlike an arraylist. More on reddit.com
๐ŸŒ r/PowerShell
38
20
January 14, 2025
Why last node remove operation in array have O(n)? - Data Structures - Code with Mosh Forum
Why removing the last node in the array is O(n) since we donโ€™t traverse the array while removing the last node and there is no left shit. More on forum.codewithmosh.com
๐ŸŒ forum.codewithmosh.com
0
June 15, 2021
Removal of the last element of an array
Is there a way to remove the last element from an array? Kind of like the function โ€œpop()โ€ in javascript More on forum.uipath.com
๐ŸŒ forum.uipath.com
3
2
August 20, 2019
How does an ArrayList remove an element from an array?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
6
10
March 15, 2023
๐ŸŒ
HackerNoon
hackernoon.com โ€บ how-to-remove-the-last-element-of-a-javascript-array
How to Remove the Last Element of a JavaScript Array | HackerNoon
November 6, 2022 - Finally, another way to accomplish this without changing the original array is to use slice (not to be confused with splice). slice() differs from both pop() and splice() in that, it makes a shallow copy of the original array. Removing the last element with slice looks like this:
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ pop
Array.prototype.pop() - JavaScript - MDN Web Docs
The removed element from the array; undefined if the array is empty. The pop() method removes the last element from an array and returns that value to the caller.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ remove last element from array in javascript
How to Remove Last Element From Array in JavaScript | Delft Stack
February 2, 2024 - Check the code below. var array = [10, 11, 12, 13, 14, 15, 16, 17]; array.splice(-1, 1); console.log(array); ... The pop() function is the most straightforward way to achieve this.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-remove-last-element-from-array
Remove last or last N elements from an Array in JavaScript | bobbyhadz
To remove the last N elements from an array, call the Array.slice() method with a start index of 0 and an end index of -N.
Find elsewhere
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ remove last element from an array in javascript
Remove last element from an array in JavaScript | Techie Delight
July 7, 2026 - This post will discuss how to remove the last element from an array in JavaScript... The standard way to remove the last element from an array is with the `pop()` method. This method works by modifying the original array.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-remove-last-array-element-in-javascript-and-return-it
JavaScript - Array pop() Method
August 26, 2022 - In JavaScript, the Array.pop() method is used to remove the last element from an array and returns that element. It modifies the original array by decreasing its length by one.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript-remove-the-last-item-from-an-array
Remove the last Item From an Array in JavaScript
November 18, 2024 - 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 Content Method 1: Using splice() MethodMethod 2: U ... The array is a type of ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how can i remove a specific item from an array?
JavaScript Remove Specific Item from Array by Index or Value | Sentry
1 week ago - If you want to remove an item from an array, you can use the pop() method to remove the last element or the shift() method to remove the first element.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-delete-last-occurrence-from-js-array
JavaScript - Delete last Occurrence from JS Array - GeeksforGeeks
July 11, 2025 - The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. ... This method uses Array.splice() method that adds/deletes items to/from the array and returns the deleted ...
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ javascript โ€บ array โ€บ remove first or last n array elements
Remove the first or last n elements from a JavaScript array - 30 seconds of code
December 24, 2023 - Conversely, in order to remove n elements from the end of an array, you can use Array.prototype.slice() with a start index of 0 and a negative end index. This will return a new array with the last n elements removed.
๐ŸŒ
Reddit
reddit.com โ€บ r/powershell โ€บ is there an easy and elegant way of removing the last element of an array?
r/PowerShell on Reddit: Is there an easy and elegant way of removing the last element of an array?
January 14, 2025 -

Edit: Solved

It's much more flexible to use generic lists instead of arrays. Arrays are immutable and should not be used when there is a need to add or remove elements. Another option is to use array lists, but others reported that they are deprecated and that generic lists should be used instead.

Thank you all for the help!

-------------

PowerShell 7, an array like $array = @()

Like the title say - is there?

The solutions I've found online are all wrong.

- Array slicing

$array = $array[0..($array.Length - 2)]

This does not work if the array length is 1, because it resolves to $array[0..-1]. Step-by-step debugging shows that instead of deleting the last remaining element of the array, it will duplicate that element. The result will be an array of 2 elements, not 0.

- Select-Object

$array = $array | Select-Object -SkipLast 1

This does not work well with Hashtables as array elements. If your array elements are Hashtables, it will convert them to System.Collections.Hashtable. Hashtable ($example = @{}) and System.Collection.Hashtable are not the same type and operations on those two types are different (with different results).

Edit for the above: There was a typo in that part of my code and it returned some nonsense results. My bad.

- System.Collections.ArrayList

Yes, you can convert an array to System.Collection.ArrayList, but you are then working with System.Collections.ArrayList, not with an array ($array = @()).

----------------

One solution to all of this is to ask if the array length is greater than one, and handle arrays of 1 and 0 elements separately. It's using an if statement to simply remove the last element of an array, which is really bad.

Another solution is to loop through an array manually and create a new one while excluding the last element.

And the last solution that I've found is not to use arrays at all and use generic lists or array lists instead.

Is one of these options really the only solution or is there something that I'm missing?

๐ŸŒ
Code with Mosh
forum.codewithmosh.com โ€บ data structures
Why last node remove operation in array have O(n)? - Data Structures - Code with Mosh Forum
June 15, 2021 - Why removing the last node in the array is O(n) since we donโ€™t traverse the array while removing the last node and there is no left shit.
๐ŸŒ
Reactgo
reactgo.com โ€บ home โ€บ how to remove the last element of an arraylist in java
How to remove the last element of an ArrayList in Java | Reactgo
December 24, 2022 - To remove the last element of a ArrayList, we can use the list.remove() method by passing its index list.size()-1 as an argument to it. The list.size()-1 gives the index of an last element.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-remove-an-element-from-a-javascript-array-removing-a-specific-item-in-js
How to Remove an Element from a JavaScript Array โ€“ Removing a Specific Item in JS
August 31, 2022 - Putting a comma before the rest operator says to avoid the first element in the array, and all the others are copied in the arrayOfCulinaryFruits array. In some cases it might be appropriate to mutate the original array. In these cases you can also use one of the following mutating methods. ... You can remove the last item of an array with Array.prototype.pop().
๐ŸŒ
Built In
builtin.com โ€บ articles โ€บ javascript-get-last-element-of-array
How to Get the Last Element of an Array in JavaScript | Built In
Alternatively, you can use the slice() method to get a new array that includes only the last element of the original array. For example: ... This will create a new array [5] containing the last element of the original array. ... The pop() method ...
๐ŸŒ
UiPath Community
forum.uipath.com โ€บ help
Removal of the last element of an array - Help - UiPath Community Forum
August 20, 2019 - Is there a way to remove the last element from an array? Kind of like the function โ€œpop()โ€ in javascript