Array.prototype.filter will create and return a new array consisting of elements that match the predicate.

function removeByIndex(array, index) {
  return array.filter(function (el, i) {
    return index !== i;
  });
}

Even shorter with ECMAScript 6:

var removeByIndex = (array, index) => array.filter((_, i) => i !== index);
Answer from c.P.u1 on Stack Overflow
🌐
Jaketrent
jaketrent.com › post › remove-array-element-without-mutating
Remove an Array Element Without Mutation
We could simply do that: create a new array (a clone of the original) and mutate that instead of the original. To do this, we could use a clone function or the spread operator (...) to copy the original array into a whole new array, the splice just the new 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().
🌐
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 - There are many different ways to do the same thing in JavaScript. In this article you have learned nine different methods to remove an element from an array. Six of them do not mutate the original array, and three of them do.
Top answer
1 of 2
20

You could use filter or reduce, or copy the array first by using slice, and then perform splice.

Personally, I like filter for its simplicity, and clear intention

filter

Copyfunction removeItem(array, n) {
  return array.filter((elem, i) => i !== n);
}

const original = [1,2,3,4];

console.log(removeItem(original, 1));
console.log(original);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

reduce

Copyfunction removeItem (array, n) {
  return array.reduce((result, elem, i) => {
    if (i !== n) result.push(elem);
    return result;
  }, [])
}

const original = [1,2,3,4];

console.log(removeItem(original, 1));
console.log(original);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

slice and splice

Copyfunction removeItem(array, n) {
  const result = array.slice();
  result.splice(n, 1);
  return result;
}

const original = [1,2,3,4];

console.log(removeItem(original, 1));
console.log(original);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Performance Test

https://jsperf.com/so53833297

Documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

2 of 2
2
Copyfunction removeItem(array, n) {
    return array.filter((x, i) => i != n)
}
🌐
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 - filter() method: This method creates a new array with all elements that pass the test implemented by the provided function. It does not mutate the original array. const array = [1, 2, 3, 4, 5]; const filteredArray = array.filter(num => num !== ...
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › array › remove elements from array
Remove elements from a JavaScript array without mutating it - 30 seconds of code
October 24, 2023 - Oftentimes, this isn't what you really want, so let's take a look at how we can implement a non-mutating version of Array.prototype.splice(). At its core, Array.prototype.splice() behaves as follows: Items between the start of the array and the given index are kept intact. Starting at the given index, the specified number of items are removed.
🌐
GitHub
gist.github.com › 6d5bd79059eaa01eadbe721b833c6e24
Remove element from array without mutation · GitHub
Remove element from array without mutation · Raw · removeWithoutMutation.js · This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Find elsewhere
🌐
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 - The combination of slice() and concat() methods allows you to remove elements from an array without mutating the original by creating a new array.
🌐
Peanutbutterjavascript
peanutbutterjavascript.com › posts › update-arrays-without-mutating-the-original
Update arrays without mutating the original
If you want to remove a specific element from the array, you can do this by using the Array.prototype.filter method. const original = [1,2, 'x', 3] const toDelete = 'x' const newArray = original.filter(item => item !== toDelete) I hope you found this resource useful when working with Immutable data in JavaScript...
Top answer
1 of 16
17015

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
2617
  • 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
🌐
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...
🌐
CoreUI
coreui.io › blog › how-to-remove-element-from-javascript-array
How to Remove Elements from a JavaScript Array · CoreUI
February 13, 2025 - One of the most popular ways to remove elements from an array in JavaScript is the splice method. This method modifies the original array directly. It takes at least two arguments (the start index and the number of elements to remove).
🌐
TutorialsPoint
tutorialspoint.com › how-to-splice-an-array-without-mutating-the-original-array
How to splice an array without mutating the original Array?
We have passed the 0 as a start index in the splice() method and three as the total number of elements to remove from the array. <html> <body> <h3> Using the <i> spread operator </i> to splice an array without mutating the original array. </h3> <div id = "output"> </div> <script> let output ...
🌐
CopyProgramming
copyprogramming.com › howto › how-to-remove-n-from-an-array-without-mutating-it
Removing an Element from an Array Without Altering It: A Guide - Javascript
June 6, 2023 - One option is to use ECMAScript 6, which offers a concise way to achieve this. Another solution involves using the spread operator to create a copy of the array, and then using splice to remove the desired element without altering the original. This approach avoids mutation of 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 delete operator deletes the object property at the specified index, but does not update the length of the array, and the value at that index of the array will be undefined. ... The splice() method takes two arguments, the index of the element ...
🌐
Medium
blog.rolloutit.net › es6-the-best-way-to-remove-elements-from-an-array-324adecb0c66
ES6 — The best way to remove elements from an array | by Hoang Nguyen Van | Rollout IT Blog
March 1, 2021 - If we need to remove few elements (less than 100 I think) let use .splice() *but only if you are 100% sure you can mutate the original array. Please, note, mutation generally not a recommended practice in JS because it can cause your code to break without your knowing about it.*
🌐
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.