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 16
17014

Find the index of the array element you want to remove using indexOf, and then remove that index with splice.

The splice() method changes the contents of an array by removing existing elements and/or adding new elements.

const array = [2, 5, 9];

console.log(array);

const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
  array.splice(index, 1); // 2nd parameter means remove one item only
}

// array = [2, 9]
console.log(array);

The second parameter of splice is the number of elements to remove. Note that splice modifies the array in place and returns a new array containing the elements that have been removed.


For completeness, here are functions. The first function removes only a single occurrence (e.g., removing the first match of 5 from [2,5,9,1,5,8,5]), while the second function removes all occurrences:

function removeItemOnce(arr, value) {
  var index = arr.indexOf(value);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
}

function removeItemAll(arr, value) {
  var i = 0;
  while (i < arr.length) {
    if (arr[i] === value) {
      arr.splice(i, 1);
    } else {
      ++i;
    }
  }
  return arr;
}
// Usage
console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
console.log(removeItemAll([2,5,9,1,5,8,5], 5))

In TypeScript, these functions can stay type-safe with a type parameter:

function removeItem<T>(arr: Array<T>, value: T): Array<T> {
  const index = arr.indexOf(value);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
}
2 of 16
2616
  • Do it simple, intuitive and explicit (Occam's razor)
  • Do it immutable (original array stays unchanged)
  • Do it with standard JavaScript functions, if your browser doesn't support them - use polyfill

In this code example I use array.filter(...) function to remove unwanted items from an array. This function doesn't change the original array and creates a new one. If your browser doesn't support this function (e.g. Internet Explorer before version 9, or Firefox before version 1.5), consider polyfilling with core-js.

Be mindful though, creating a new array every time takes a big performance hit. If the list is very large (think 10k+ items) then consider using other methods.

Removing item (ECMA-262 Edition 5 code AKA old style JavaScript)

var value = 3

var arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(function(item) {
    return item !== value
})

console.log(arr)
// [ 1, 2, 4, 5 ]

Removing item (ECMAScript 6 code)

let value = 3

let arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(item => item !== value)

console.log(arr)
// [ 1, 2, 4, 5 ]

IMPORTANT ECMAScript 6 () => {} arrow function syntax is not supported in Internet Explorer at all, Chrome before version 45, Firefox before version 22, and Safari before version 10. To use ECMAScript 6 syntax in old browsers you can use BabelJS.


Removing multiple items (ECMAScript 7 code)

An additional advantage of this method is that you can remove multiple items

let forDeletion = [2, 3, 5]

let arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(item => !forDeletion.includes(item))
// !!! Read below about array.includes(...) support !!!

console.log(arr)
// [ 1, 4 ]

IMPORTANT array.includes(...) function is not supported in Internet Explorer at all, Chrome before version 47, Firefox before version 43, Safari before version 9, and Edge before version 14 but you can polyfill with core-js.

Removing multiple items (in the future, maybe)

If the "This-Binding Syntax" proposal is ever accepted, you'll be able to do this:

// array-lib.js

export function remove(...forDeletion) {
    return this.filter(item => !forDeletion.includes(item))
}

// main.js

import { remove } from './array-lib.js'

let arr = [1, 2, 3, 4, 5, 3]

// :: This-Binding Syntax Proposal
// using "remove" function as "virtual method"
// without extending Array.prototype
arr = arr::remove(2, 3, 5)

console.log(arr)
// [ 1, 4 ]

Try it yourself in BabelJS :)

Reference

  • Array.prototype.includes
  • Functional composition
🌐
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} />
            ))}
  	</>
    )
}
🌐
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.
🌐
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
The splice() method takes two arguments, the index of the element you wish to remove and the index you wish to remove up to.
🌐
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