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 - Lastly, the most common array method for removing elements is pop(). The popmethod removes the last element of the array, decrements the length, and returns the element that was removed. It’s important to note that this method modifies the original array that it is invoked on. ... That just about wraps it up. I hope you have learned a few additional ways to remove the last item from an array, without having to rely on just one approach. Stay tuned for more articles on JavaScript!
🌐
Jaketrent
jaketrent.com › post › remove-array-element-without-mutating
Remove an Array Element Without Mutation
August 18, 2017 - You can also think of this as creating a new array without the elements you wanted removed. We could simply do that: create a new array (a clone of the original) and mutate that instead of the original.
🌐
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 - This is the same approach we use in CoreUI components to manage navigation history and dynamic content stacks. The pop() method is highly optimized for removing from the end, making it the preferred choice over alternatives like slice(0, -1) ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › pop
Array.prototype.pop() - JavaScript | MDN
In case you want the value of this to be the same, but return a new array with the last element removed, you can use arr.slice(0, -1) instead. The pop() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, ...
🌐
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.
🌐
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.
🌐
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().
Find elsewhere
🌐
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. To create a new array with a segment removed and/or replaced without mutating the original array, use toSpliced().
🌐
Enterprise DNA
blog.enterprisedna.co › how-to-remove-the-last-array-element-in-javascript
How to Remove the Last Array Element in JavaScript: 4 Ways – Master Data Skills + AI
You can remove the last element of an array in JavaScript using the pop() method: To remove the last element of an array without mutating existing elements in the original array, you can use the slice() method:
🌐
DhiWise
dhiwise.com › post › understanding-how-to-remove-element-from-array-javascript
How to Remove Element from Array JavaScript: Best Practices
February 1, 2024 - The rest operator can create a new array for more advanced techniques by omitting specific elements, without mutating the original array. A custom function can handle negative indices to remove elements from the end of an array.
🌐
Medium
medium.com › @bosti › remove-a-specific-item-from-an-array-in-javascript-bfe45cdd5894
Remove a specific item from an array in JavaScript | by Bostiman | Medium
April 14, 2023 - It does not mutate the original array. const array = [1, 2, 3, 4, 5]; const indexToRemove = 2; const newArray = [...array.slice(0, indexToRemove), ...array.slice(indexToRemove + 1)]; console.log(newArray); // [1, 2, 4, 5] Another way to remove an element from an array is by mutating the original array.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-splice-an-array-without-mutating-the-original-array
How to Splice an Array Without Mutating the Original Array? | GeeksforGeeks
November 19, 2024 - Here are the most effective ways to do this · The combination of slice() and concat() methods allows you to remove elements from an array without mutating the original by creating a new array.
Top answer
1 of 15
12

What a shame you have an array of integers, not an object where the keys are string equivalents of these integers.

I've looked through a lot of these answers and they all seem to use "brute force" as far as I can see. I haven't examined every single one, apologies if this is not so. For a smallish array this is fine, but what if you have 000s of integers in it?

Correct me if I'm wrong, but can't we assume that in a key => value map, of the kind which a JavaScript object is, that the key retrieval mechanism can be assumed to be highly engineered and optimised? (NB: if some super-expert tells me that this is not the case, I can suggest using ECMAScript 6's Map class instead, which certainly will be).

I'm just suggesting that, in certain circumstances, the best solution might be to convert your array to an object... the problem being, of course, that you might have repeating integer values. I suggest putting those in buckets as the "value" part of the key => value entries. (NB: if you are sure you don't have any repeating array elements this can be much simpler: values "same as" keys, and just go Object.values(...) to get back your modified array).

So you could do:

Copyconst arr = [ 1, 2, 55, 3, 2, 4, 55 ];
const f =    function( acc, val, currIndex ){
    // We have not seen this value before: make a bucket... NB: although val's typeof is 'number',
    // there is seamless equivalence between the object key (always string)
    // and this variable val.
    ! ( val in acc ) ? acc[ val ] = []: 0;
    // Drop another array index in the bucket
    acc[ val ].push( currIndex );
    return acc;
}
const myIntsMapObj = arr.reduce( f, {});

console.log( myIntsMapObj );

Output:

Object [ <1 empty slot>, Array1, Array[2], Array1, Array1, <5 empty slots>, 46 more… ]

It is then easy to delete all the numbers 55.

Copydelete myIntsMapObj[ 55 ]; // Again, although keys are strings this works

You don't have to delete them all: index values are pushed into their buckets in order of appearance, so (for example):

CopymyIntsMapObj[ 55 ].shift(); // And
myIntsMapObj[ 55 ].pop();

will delete the first and last occurrence respectively. You can count frequency of occurrence easily, replace all 55s with 3s by transferring the contents of one bucket to another, etc.

Retrieving a modified int array from your "bucket object" is slightly involved but not so much: each bucket contains the index (in the original array) of the value represented by the (string) key. Each of these bucket values is also unique (each is the unique index value in the original array): so you turn them into keys in a new object, with the (real) integer from the "integer string key" as value... then sort the keys and go Object.values( ... ).

This sounds very involved and time-consuming... but obviously everything depends on the circumstances and desired usage. My understanding is that all versions and contexts of JavaScript operate only in one thread, and the thread doesn't "let go", so there could be some horrible congestion with a "brute force" method: caused not so much by the indexOf ops, but multiple repeated slice/splice ops.

Addendum If you're sure this is too much engineering for your use case surely the simplest "brute force" approach is

Copyconst arr = [ 1, 2, 3, 66, 8, 2, 3, 2 ];
const newArray = arr.filter( number => number !== 3 );
console.log( newArray )

(Yes, other answers have spotted Array.prototype.filter...)

2 of 15
11

Remove one value, using loose comparison, without mutating the original array, ES6

Copy/**
 * Removes one instance of `value` from `array`, without mutating the original array. Uses loose comparison.
 *
 * @param {Array} array Array to remove value from
 * @param {*} value Value to remove
 * @returns {Array} Array with `value` removed
 */
export function arrayRemove(array, value) {
    for(let i=0; i<array.length; ++i) {
        if(array[i] == value) {
            let copy = [...array];
            copy.splice(i, 1);
            return copy;
        }
    }
    return array;
}
🌐
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 - One of the most frequent operations we perform on an array is removing the last element. There are a few different ways to do this - but one of the most common
🌐
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 » ·
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-delete-last-occurrence-from-js-array
JavaScript - Delete last Occurrence from JS Array - GeeksforGeeks
July 11, 2025 - To remove the last element from an array, you can use the spread operator along with array destructuring. ... The reduceRight() method applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to ...
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › pop
JavaScript Array pop() - Remove Last Element | Vultr Docs
December 9, 2024 - This snippet continuously removes elements from the scores array as long as the last element of the array is 80 or more. It efficiently uses pop() to both alter the array and supply the conditional loop with the required checks. The pop() method in ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-delete-last-occurrence-from-js-array
JavaScript – Delete last Occurrence from JS Array | GeeksforGeeks
November 18, 2024 - Using pop() Method (Simple and Easiest Method for Any Array) 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.