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
August 18, 2017 - You can avoid a whole class of bugs. You can create more readable, predictable programs. To remove elements from an array in an immutable way, array copying is required. You need a new data structure because you cannot change the first -- that would be mutation.
๐ŸŒ
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().
๐ŸŒ
CoreUI
coreui.io โ€บ answers โ€บ how-to-remove-the-first-item-from-an-array-in-javascript
How to remove the first item from an array in JavaScript ยท CoreUI
September 18, 2025 - In this example, fruits.shift() removes โ€˜appleโ€™ from the beginning of the array and returns it, while the array becomes ['banana', 'orange']. The method returns the removed element, which you can capture in a variable if needed, or simply call fruits.shift() to remove without storing the value. This is the same approach we use in CoreUI components to manage notification queues and dynamic navigation elements. For performance-critical applications with large arrays, consider using slice(1) to create a new array instead of mutating the original, though shift() remains the standard choice for most scenarios.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-splice-an-array-without-mutating-the-original-array
How to Splice an Array Without Mutating the Original Array? - GeeksforGeeks
July 23, 2025 - 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.
๐ŸŒ
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.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ shift
Array.prototype.shift() - JavaScript | MDN
In case you want the value of this to be the same, but return a new array with the first element removed, you can use arr.slice(1) instead. The shift() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, ...
Find elsewhere
๐ŸŒ
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?
๐ŸŒ
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.
๐ŸŒ
DEV Community
dev.to โ€บ jsdevspace โ€บ 9-ways-to-remove-elements-from-arrays-in-javascript-4be6
9 Ways to Remove Elements from Arrays in JavaScript - DEV Community
August 6, 2024 - You can use the spread operator along with slice() or filter() to create a new array without a specific element.
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ understanding-how-to-remove-element-from-array-javascript
How to Remove Element from Array JavaScript: Best Practices
February 1, 2024 - You can effectively manage and manipulate the first array element using the shift method. However, remember that, like the pop method, the shift also mutates the original array. While methods like splice, delete, filter, pop, and shift are common ...
๐ŸŒ
Love2Dev
love2dev.com โ€บ blog โ€บ javascript-remove-from-array
9 Ways To Remove ๐Ÿ—‘๏ธ Elements From A JavaScript Array ๐Ÿ“‡[Examples]
The shift method works much like the pop method except it removes the first element of a JavaScript array instead of the last.
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;
}
๐ŸŒ
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. The given items, if any, are inserted after the given index. The rest of the items are kept intact. Having broken down this behavior, we can easily see that we can implement it using Array.prototype.slice() and Array.prototype.concat().
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_shift.asp
JavaScript Array shift() Method
โฎ Previous JavaScript Array Reference Next โฏ ยท Shift (remove) the first element of the array: const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift(); Try it Yourself ยป ยท The shift() method returns the shifted element: const ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ why is it that removing an element from the start of an array o(n) while at the end o(1)?
r/learnprogramming on Reddit: Why is it that removing an element from the start of an array O(n) while at the end O(1)?
September 16, 2023 -

From my understanding the reason why removing the last element is O(1) is because you don't need to shift the array in memory. You simply remove the last element and leave the old space empty. So why is it that if you remove the first element that the Array HAS to to shift in memory (making it O(n))?

I don't understand the reasoning, if we are okay with leaving empty space in memory at the end of an array and not shifting all the other things surrounding the array in memory. Then why do we have to shift the array in memory if there is space that the start?

I am not understanding, if it's because the memory is trying to stay compact and no empty spaces are allowed. Then why don't all the other stuff in memory be shifted to the left after new space was cleared once we removed the last element from the array?

๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 496498 โ€บ remove-the-first-element-of-an-array
Remove the first element of an array | Sololearn: Learn to code for FREE!
June 28, 2017 - Therefore it removes 1 element starting from the index 0 i.e. number at index 0 . ... html> <head> <title>Empty array</title> </head> <body> <script> var empArr=[]; var j=empArr.length; var s; for(var i=1,j=2500;i<j;i++) { var rand=empArr.push(Math.round(Math.random() * 2500)) console.log(rand); } function arr(){ for(var i=1;i<j;i++) {if(j>=2500){ s=empArr.shift(); }return empArr[0]; } } document.write(arr()); </script> </body> </html>