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
🌐
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 - The pop() method is highly optimized for removing from the end, making it the preferred choice over alternatives like slice(0, -1) when you need to mutate the original array. ... Follow Łukasz Holeczek on GitHub Connect with Łukasz Holeczek ...
🌐
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, ...
🌐
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 - You will often need to remove an element from an array in JavaScript, whether it's for a queue data structure, or maybe from your React State. In the first half of this article you will learn all the methods that allow you to remove an element from an array without mutating the original 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. To create a new array with a segment removed and/or replaced without mutating the original array, use toSpliced().
🌐
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).
🌐
Jaketrent
jaketrent.com › post › remove-array-element-without-mutating
Remove an Array Element Without Mutation
Here's a way to remove an array element without mutating the array. By default, the array methods in JavaScript will mutate your array. Mutation means that the original array is changed in place.
Find elsewhere
🌐
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
To remove the last element of an array without mutating existing elements in the original array, you can use the slice() method: ... A hands-on project focused on understanding and implementing scalable architecture using React components.
🌐
CoreUI
coreui.io › blog › how-to-remove-element-from-javascript-array
How to Remove Elements from a JavaScript Array · CoreUI
February 13, 2025 - Removing elements from an array in JavaScript is a fundamental skill every developer should master. Whether you need to remove the first element, remove the last element, or filter out unwanted elements based on a specified value, understanding multiple techniques to remove array elements ensures ...
🌐
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 - Did you know there are multiple ways to remove an element from an array? Let's take a look. ... Did you know that implementing a non-mutating version of Array.prototype.splice() is only a few lines of code?
🌐
CopyProgramming
copyprogramming.com › howto › javascript-reactjs-remove-last-element-from-array
Removing the Last Element from a Javascript ReactJS Array - Javascript
April 29, 2023 - How to Remove an Element from a JavaScript Array, A final method to remove an element from an array without mutating the original array is by using the push method.
🌐
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 - When you use splice() to modify an array in Svelte, such as adding or removing items, simply calling the method won't trigger a UI update because Svelte relies on reactivity, which requires the array to be reassigned. This article explores two ways to ensure the array updates correctly after a splic ... In JavaScript, slice() and splice() are array methods with distinct purposes. `slice()` creates a new array containing selected elements from the original, while `splice()` modifies the original array by adding, removing, or replacing elements.
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;
}
🌐
Reddit
reddit.com › r/reactjs › item is removed correctly from array, but visually only last item in array is removed?
r/reactjs on Reddit: Item is removed correctly from array, but visually only last item in array is removed?
November 22, 2021 -

Hi, I'm mapping over an array to create multiple components and also pass a remove function as a prop to remove itself from the array. The devtools tell me it's working correctly, but visually it is always removing the last element and I have no idea why. I have tried many different ways, but never found a working solution. The code below is a simplified version of my current attempt. Please help!

const array = [
    { id: 0, title: 'one'},
    { id: 1, title: 'two'},
    { id: 2, title: 'three'},
]

const App = () => {
    const [items, setItems] = useState(array)
  
    const removeItem = (id) => {
        const newItems = items.filter(item => item.id !== id)
        setItems(newItems)
    }
  
    return (
        <>
            {items.map(item => (
      	        <Component id={item.id} removeItem={removeItem} />
            ))}
  	</>
    )
}
🌐
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
🌐
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.
🌐
IQCode
iqcode.com › code › javascript › how-to-replace-array-element-in-javascript-without-mutation
how to replace array element in javascript without mutation Code Example
function replaceAt(array, index, value) { const ret = array.slice(0); ret[index] = value; return ret; } const newArray = replaceAt(ite...