Is there a method to remove an item from a JavaScript array?
Yes, there are several ways to remove items from an array in JavaScript. Which one you choose often depends on whether you need to remove a specific item by index, a specific item by value, or you simply want to remove the first or last element. Below are the most common methods:
1. Removing by Index with splice()
splice()
allows you to remove (or insert) items at a specific index:
const fruits = ["apple", "banana", "cherry", "date"]; // Remove 1 item starting from index 1 fruits.splice(1, 1); console.log(fruits); // Output: ["apple", "cherry", "date"]
- Explanation:
splice(startIndex, deleteCount, ...itemsToAdd)
. - Pros: Mutates the array in place and gives you full control over exactly which indices are affected.
- Cons: Requires you to know the index beforehand.
2. Removing by Value with filter()
If you only know the value you want to remove, you can use filter()
to create a new array without that value:
const fruits = ["apple", "banana", "cherry", "banana"]; const result = fruits.filter((fruit) => fruit !== "banana"); console.log(result); // Output: ["apple", "cherry"]
- Pros: Clear, functional approach that doesn’t mutate the original array.
- Cons: Creates a new array; the original
fruits
remains unchanged.
3. Removing the Last Element with pop()
If you just need to remove the last item:
const fruits = ["apple", "banana", "cherry"]; const removedItem = fruits.pop(); console.log(removedItem); // "cherry" console.log(fruits); // ["apple", "banana"]
- Pros: Very efficient,
pop()
is O(1) in most JavaScript engines. - Cons: Only works for the last element.
4. Removing the First Element with shift()
If you need to remove the first item:
const fruits = ["apple", "banana", "cherry"]; const removedItem = fruits.shift(); console.log(removedItem); // "apple" console.log(fruits); // ["banana", "cherry"]
- Pros: Straightforward if you consistently remove from the start.
- Cons: O(n) because JavaScript must shift all other elements to new indices.
5. Removing by Value with indexOf()
+ splice()
To remove an item by value in place, find its index with indexOf()
(or findIndex()
for more complex conditions), then remove it using splice()
:
const fruits = ["apple", "banana", "cherry", "banana"]; const index = fruits.indexOf("banana"); if (index !== -1) { fruits.splice(index, 1); } console.log(fruits); // Possible Output: ["apple", "cherry", "banana"]
- Pros: Mutates the array in place, removing exactly one occurrence.
- Cons: If you need to remove all occurrences of a value, you’ll need a loop or multiple calls to
indexOf()
+splice()
.
Choosing the Right Approach
- Single Item by Value?
indexOf()
orfindIndex()
+splice()
, or usefilter()
if you don’t mind creating a new array. - Multiple Items by Condition?
filter()
is often cleaner (though non-mutating). - First or Last Item?
shift()
orpop()
is simplest. - Performance Considerations:
pop()
is usually O(1).shift()
can be O(n).splice()
can also lead to reindexing in the worst case but is flexible for in-place modifications.filter()
returns a new array (O(n)) and doesn’t mutate the original.
Level Up Your JavaScript Skills
If you’d like a deeper understanding of core JavaScript and modern best practices, consider taking this course from DesignGurus.io:
- Grokking JavaScript Fundamentals – Strengthen your knowledge of modern JS features, DOM manipulation, and coding styles to write cleaner, more efficient code.
For more advanced interview prep, you can explore:
- Grokking the Coding Interview: Patterns for Coding Questions – Learn the most frequent coding patterns that come up in interviews, saving you time in solving new problems.
You might also check out the DesignGurus YouTube Channel for free tutorials or sign up for a Coding Mock Interview to get personalized feedback from ex-FAANG engineers.
Final Takeaway: There is no single “remove” method for arrays in JavaScript, but you can easily remove items using splice()
, filter()
, pop()
, shift()
, or a combination of indexOf()
+ splice()
. Each approach has different trade-offs regarding mutation vs. immutability, performance, and coding style.