How to remove item from array by value?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In JavaScript, removing an array item by value usually means one of two things: remove the first matching value from the existing array, or build a new array without that value. Which approach is correct depends on whether you want to mutate the original array.
That distinction matters because JavaScript arrays are reference types. If other code holds the same array object, an in-place change affects everyone who can see it.
Remove the First Matching Value In Place
If you want to change the existing array, the classic approach is indexOf() plus splice():
indexOf() returns the first matching index or -1 if the value is not present. splice(index, 1) removes one element starting at that index.
This is the most direct answer when you want to remove only the first occurrence.
Remove All Matching Values Without Mutating
If you want a new array and you do not want to touch the original one, filter() is usually cleaner:
This returns a new array containing only the elements that passed the test. It is often preferred in React, Redux, and other codebases where immutable updates are easier to reason about.
Removing Objects by Value-Like Criteria
Arrays often hold objects, not primitive values. In that case, indexOf() only works if you pass the exact same object reference.
For example, this usually does not work the way people expect:
That fails because the new object literal is not the same reference as the object inside the array.
Instead, search by a property with findIndex():
This is the right pattern when "by value" really means "by matching object fields."
A Reusable Helper
If you need this often, wrap it in a helper function:
And if you prefer an immutable helper:
The helper names make the behavior explicit, which is useful when both mutation styles exist in the same codebase.
Why delete Is Usually Wrong
New JavaScript developers sometimes try:
delete removes the property at that index, but it does not shift the array or reduce its length. You end up with a sparse array containing an empty slot.
If your goal is actual removal, use splice() or create a filtered copy.
Performance and Behavior Tradeoffs
splice() is good when you intentionally mutate the existing array and only need to remove one element. filter() is cleaner when you want every matching value removed or when immutable updates fit the architecture better.
Both operations are still linear in the size of the array because JavaScript may need to inspect each element and shift later elements.
For very large collections where frequent removals matter, a different data structure such as Map or Set might be a better fit than an array.
Common Pitfalls
The biggest pitfall is forgetting that indexOf() only removes the first match. If the value appears several times, the rest stay in the array unless you use filter() or loop intentionally.
Another common mistake is using delete and expecting the array length to shrink. It will not.
Developers also get tripped up by object identity. indexOf() compares object references, not object contents, so matching an object "by value" requires findIndex() or filter() with a predicate.
Finally, choose mutation deliberately. splice() changes the original array, while filter() returns a new one. Using the wrong version can create subtle bugs in UI state management.
Summary
- Use
indexOf()plussplice()to remove the first matching primitive value in place. - Use
filter()when you want a new array or need to remove all matches. - For arrays of objects, use
findIndex()orfilter()with a predicate instead ofindexOf(). - Avoid
deletefor array removal because it leaves holes. - Decide explicitly whether the operation should mutate the original array or return a new one.

