How to Remove an Item From an Array by Value in JavaScript
Remove by value with filter for a new array, or indexOf plus splice to change it in place. See the splice -1 trap, NaN, objects and removing inside loops.

JavaScript has no array.remove(value) method, and the replacements come in two families that behave differently. One builds a new array without the value, and leaves the original alone. The other finds the item's position and deletes it from the array itself. Each has a version that removes only the first match and one that removes all of them, and each has a trap: removing in place with splice goes wrong when the value is not found, because -1 is a valid position, and removing inside a loop skips items. This post shows the main methods with exact output, the decision between making a new array and changing the existing one, how to remove every occurrence safely, what happens with NaN and with objects, and the mistakes that look right in review. The examples run on Node.js 24.
Make a new array with filter
const letters = ["a", "b", "c", "b", "d"];
const withoutB = letters.filter((letter) => letter !== "b");
console.log(withoutB);
console.log(letters);[ 'a', 'c', 'd' ]
[ 'a', 'b', 'c', 'b', 'd' ]filter keeps the items for which the callback returns true, so asking for those that differ from the value removes every occurrence. The original array is untouched, which makes this the safest choice: no other part of the program sees its data change. It is the usual pattern for state that must not be mutated, such as state in a UI library or a value that was passed in as an argument. It builds a new array every time, so it costs memory proportional to the size, which is rarely a concern.
Change the array itself with indexOf and splice
const items = [1, 2, 3, 2, 4];
const index = items.indexOf(2);
const removed = items.splice(index, 1);
console.log(items, removed, index);[ 1, 3, 2, 4 ] [ 2 ] 1indexOf finds the position of the first match, and splice(index, 1) removes one item at that position, changing the array and returning the removed items in a new array. Only the first 2 was removed: the second one is still there. Use this when you hold the one array that everyone shares and must change it in place, for example a list stored on a long-lived object.
The trap: indexOf returns -1
const list = [1, 2, 3, 4];
const missing = list.indexOf(99);
console.log(missing);
const wrong = list.splice(missing, 1);
console.log(wrong, list);-1
[ 4 ] [ 1, 2, 3 ]When the value is not in the array, indexOf returns -1, and splice(-1, 1) is a valid call: a negative start counts from the end, so it removed the last item, 4, which had nothing to do with the value you were looking for. The code ran without an error and deleted data. This is the most common bug in this area. Always test for -1 before calling splice.
function removeFirst(array, value) {
const position = array.indexOf(value);
if (position === -1) return false;
array.splice(position, 1);
return true;
}
const data = ["x", "y", "z"];
console.log(removeFirst(data, "y"), data);
console.log(removeFirst(data, "nope"), data);true [ 'x', 'z' ]
false [ 'x', 'z' ]The helper reports whether it removed something and leaves the array alone when the value is missing. Returning a boolean lets the caller react, for example to show a "not found" message.
Remove every occurrence in place
A single indexOf finds only the first match. To remove all of them from the array itself, walk backward.
function removeAll(array, value) {
for (let index = array.length - 1; index >= 0; index--) {
if (array[index] === value) array.splice(index, 1);
}
return array;
}
const numbers = [2, 1, 2, 2, 3, 2];
console.log(removeAll(numbers, 2));
console.log(numbers);[ 1, 3 ]
[ 1, 3 ]Going from the end means that removing an item shifts only the items after it, which the loop has already passed, so no index is skipped. The function returns the same array, now changed, which is why both lines print the same contents. Going forward breaks, as the next section shows. For a large array, a single pass that writes the kept items into the front and then shortens the array is faster than many splice calls, since each splice moves the items after it, and we only mention the mechanism and not a figure.
The trap: removing while iterating forward
const forward = [2, 2, 3, 2];
for (let index = 0; index < forward.length; index++) {
if (forward[index] === 2) forward.splice(index, 1);
}
console.log(forward);
const viaForOf = [2, 2, 3];
for (const value of viaForOf) {
if (value === 2) viaForOf.splice(viaForOf.indexOf(value), 1);
}
console.log(viaForOf);[ 2, 3 ]
[ 2, 3 ]After the first splice, the next item slides into the current index, and the loop moves on to the following index, so the item that slid down is never examined. One of the three 2 values survived in the first array, and one of the two in the second. forEach and for...of have the same problem. Use filter, or iterate backward, when you remove while looping.
Without changing the original: toSpliced
toSpliced is the copying version of splice, available in Node.js 20 and later and in current browsers.
const original = ["a", "b", "c", "d"];
const without = original.toSpliced(original.indexOf("c"), 1);
console.log(without, original);
const position = original.indexOf("zzz");
console.log(position === -1 ? original : original.toSpliced(position, 1));
console.log(original.toSpliced(-1, 1));[ 'a', 'b', 'd' ] [ 'a', 'b', 'c', 'd' ]
[ 'a', 'b', 'c', 'd' ]
[ 'a', 'b', 'c' ]toSpliced returns a new array with the change applied and leaves the original alone, so it fits places where mutation is not allowed but you have an index. The last line shows that it has the same -1 behavior as splice: a negative index counts from the end. Guard against -1 here too, as the second line does.
NaN, and why indexOf cannot find it
const values = [1, NaN, 3, NaN];
console.log(values.indexOf(NaN), values.includes(NaN), values.findIndex(Number.isNaN));
console.log(values.filter((value) => !Number.isNaN(value)));
console.log(values.filter((value) => value !== NaN));-1 true 1
[ 1, 3 ]
[ 1, NaN, 3, NaN ]indexOf and !== use strict equality, and NaN is not equal to itself, so neither can find or exclude it: the filter with !== NaN removed nothing. includes() uses SameValueZero, which treats NaN as equal to itself, and findIndex(Number.isNaN) finds the position. To remove NaN, filter with Number.isNaN, or with Object.is(value, NaN) for a general version.
Objects: by reference or by field
Two objects with the same fields are different objects, so removing "the same" object needs a decision.
const users = [
{ id: 1, name: "Ada" },
{ id: 2, name: "Grace" },
{ id: 3, name: "Linus" },
];
const target = users[1];
console.log(users.indexOf({ id: 2, name: "Grace" }));
console.log(users.indexOf(target));
console.log(users.filter((user) => user !== target).map((user) => user.name));
console.log(users.filter((user) => user.id !== 3).map((user) => user.name));
const at = users.findIndex((user) => user.name === "Ada");
if (at !== -1) users.splice(at, 1);
console.log(users.map((user) => user.name));-1
1
[ 'Ada', 'Linus' ]
[ 'Ada', 'Grace' ]
[ 'Grace', 'Linus' ]indexOf of a new object literal is -1, because it is a different object. Remove by reference when you hold the object, or by a field, with a predicate, when you have an identifier. findIndex takes a predicate and returns the position of the first match, and it is the pair of splice for arrays of objects. When you remove by field, the question of "first or all" returns: filter removes all with that id, and findIndex with splice removes the first.
delete leaves a hole
const sparse = [1, 2, 3];
delete sparse[1];
console.log(sparse, sparse.length, 1 in sparse);
console.log(sparse.filter(() => true));[ 1, <1 empty item>, 3 ] 3 false
[ 1, 3 ]The delete operator removes the property and leaves an empty slot, and the array keeps its length. This is almost never what you want from "remove an item". splice and filter close the gap. Note that filter skips holes, so it also repairs a sparse array, which is a side effect and not a reason to use delete.
When the items are unique: use a Set
const tags = new Set(["news", "tech", "sport"]);
console.log(tags.delete("tech"), tags.delete("missing"), [...tags]);
console.log(tags.has("news"), tags.size);true false [ 'news', 'sport' ]
true 2If the collection never holds a value twice, a Set is a better structure than an array: delete removes a value by value, returns whether it was there, and does so in time that does not grow with the size. Converting a list that needs frequent removals into a set is often the simplest fix for slow code, and the post on removing duplicates shows how to deduplicate first.
A mistake we hit: removing the wrong item
function unsubscribe(listeners, handler) {
listeners.splice(listeners.indexOf(handler), 1);
}
const first = () => "first";
const second = () => "second";
const third = () => "third";
const listeners = [first, second, third];
unsubscribe(listeners, () => "not registered");
console.log(listeners.map((fn) => fn()));
function safeUnsubscribe(list, handler) {
const index = list.indexOf(handler);
if (index !== -1) list.splice(index, 1);
}
safeUnsubscribe(listeners, () => "not registered");
safeUnsubscribe(listeners, first);
console.log(listeners.map((fn) => fn()));[ 'first', 'second' ]
[ 'second' ]The first function was called with a handler that was never registered, which is easy to do with an inline arrow function, since a new function is a different object. indexOf returned -1, splice(-1, 1) removed the last listener, and third stopped receiving events without any error. The safe version checks the index. The same mistake in a component that called its cleanup twice removed a listener that belonged to someone else.
Choosing between the methods
| You want | Use |
|---|---|
| A new array without every match | array.filter((item) => item !== value) |
| A new array without the item at a known index | array.toSpliced(index, 1) |
| Remove the first match in place | indexOf, a check for -1, then splice(index, 1) |
| Remove every match in place | A backward loop with splice |
| Remove by a field of an object | filter by the field, or findIndex then splice |
Remove NaN | filter(Number.isNaN) negated |
| Unique items with frequent removals | A Set and delete |
Practical rules we follow
Prefer filter and treat arrays as values. When you must change in place, check for -1 before splice. Remove backward, never forward, inside a loop. Use findIndex for objects, and a predicate for fields. Do not use delete on array items. Use includes() or Number.isNaN for NaN. And write a small helper with a boolean result, so the rule lives in one place.
Where these methods stop
Removing by value is a linear scan, so doing it many times on a large array is slow, and a Set or a Map is the fix. Removing by value cannot tell two equal-looking objects apart without a predicate. Arrays that are shared across modules and changed in place invite bugs that are hard to trace, since any code can change them at any time. The reference is in the MDN Array.prototype.splice page and the MDN Array.prototype.filter page.


