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"]

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 - Here, we simply begin passing in 0 as our first argument (which determines where to start the slice) and arr.length — 1 as our last argument (the index of the first element to exclude from the returned array). ... Lastly, the most common array method for removing elements is pop().
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
javascript - Remove the last item from an array (and not return item) - Stack Overflow
Splice and pop will both return the removed element. They will mutate the original array, but if you are looking for a function that returns the array without the removed element, use slice. More on stackoverflow.com
🌐 stackoverflow.com
[Javascript] Remove last 2 elements of array? slice/splice not working
Splice() modifies the original array and returns an array of the removed values. So when you do testimony = testimony.trim().split().splice(), you’re setting testimony to the items that were removed by the splice(). Instead you set testimony.trim().split() to another variable, which would then get modified by the splice(). More on reddit.com
🌐 r/learnprogramming
1
1
February 19, 2021
Why is removing a specific element from an array so needlessly complicated in Javascript?
You could use the .filter method. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter Use it like this: const newArray = array.filter(element => element > 6) The 'element > 6' is just a placeholder example. If your array was a series of numbers, it would only return numbers that are greater than six. If you wanted it to remove a certain word from an array of words you could switch that conditional statement out with "element !== 'word' " 'newArray' will be your array without the value you're trying to remove. 'Array' represents the current array with the value you're trying to remove More on reddit.com
🌐 r/learnprogramming
9
0
January 17, 2023
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › pop
Array.prototype.pop() - JavaScript | MDN
The pop() method of Array instances removes the last element from an array and returns that element. This method changes the length of the array.
🌐
CoreUI
coreui.io › answers › how-to-remove-the-last-item-from-an-array-in-javascript
How to remove the last item from an array in JavaScript · CoreUI
September 18, 2025 - From my extensive expertise, the ... and designed specifically for this common use case. Use the pop() method to remove and return the last item from an array....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-remove-n-elements-from-the-end-of-a-given-array-in-javascript
JavaScript - Remove n Elements From End Array - GeeksforGeeks
July 23, 2025 - JavaScript · let a = ["a", "b", ... ] In this example, a.splice(-n, n) removes n elements starting from the -n index. The pop() method is used to remove the last element from the array....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › findLast
Array.prototype.findLast() - JavaScript | MDN
January 28, 2026 - It calls a provided callbackFn function once for each element in an array in descending-index order, until callbackFn returns a truthy value. findLast() then returns that element and stops iterating through the array. If callbackFn never returns a truthy value, findLast() returns undefined.
🌐
Sentry
sentry.io › sentry answers › javascript › how can i remove a specific item from an array?
How Can I Remove a Specific Item from an Array? | Sentry
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.
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_array_methods.asp
JavaScript Array Methods
Popping items out of an array, or pushing items into an array. The pop() method removes the last element from an array:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › splice
Array.prototype.splice() - JavaScript | MDN
The splice() method of Array instances changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
🌐
TutorialsPoint
tutorialspoint.com › how-to-remove-last-array-element-in-javascript-and-return-it
How to remove last array element in JavaScript and return it?
August 26, 2022 - We have learned two methods, using which we can remove the last array element in JavaScript. Among these methods, pop() is the easiest way to remove the last element of an array.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › reduce
Array.prototype.reduce() - JavaScript | MDN
Removing duplicate items in an array. Use Set and Array.from() instead. ... Eliminating or adding elements in an array.
🌐
W3Schools
w3schools.com › jsref › jsref_pop.asp
JavaScript Array pop() Method
❮ Previous JavaScript Array Reference Next ❯ · Remove (pop) the last element: const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop(); Try it Yourself » · pop() returns the element it removed: const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop(); Try it Yourself » ·
🌐
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?

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-delete-last-occurrence-from-js-array
JavaScript - Delete last Occurrence from JS Array - GeeksforGeeks
July 11, 2025 - The spread operator (...) is a concise way to manipulate arrays. To remove the last element from an array, you can use the spread operator along with array destructuring.
🌐
Just Academy
justacademy.co › blog-detail › how-to-remove-last-element-from-array-in-javascript
How to Remove Last Element from Array in JavaScript
Removing the last element from an array in JavaScript can be useful in scenarios where you want to update the array or process the elements in reverse order. This operation can be achieved by using the array's `pop()` method, which removes the ...
🌐
DEV Community
dev.to › codeanddeploy › how-to-remove-last-element-from-array-in-javascript-d22
How to Remove Last Element from Array in Javascript? - DEV Community
March 29, 2022 - If you need to remove the last value of the array before processing the data then array.pop() done this. ... <script> var websites = ['google.com', 'facebook.com', 'youtube.com']; websites.pop(); console.log(websites); // result: ["google.com", ...
🌐
Scaler
scaler.com › home › topics › remove elements from a javascript array
Remove Elements from a JavaScript Array - Scaler Topics
March 12, 2024 - The pop() method is used to removes the last element from an array in JavaScript.