Last modified: Jul 27, 2026

JavaScript Remove Item from Array Guide

Arrays are a core part of JavaScript. They store lists of data. But what happens when you need to remove an item? This guide shows you how. You will learn multiple ways to remove elements. Each method has a specific use case. We will cover them all step by step.

Why Remove Items from an Array?

Removing items keeps your data clean. It helps manage dynamic lists. For example, you might delete a user from a list. Or remove a completed task. Understanding removal methods is key to efficient coding. It also prevents bugs and memory issues.

Method 1: Using pop()

The pop() method removes the last element from an array. It also returns that element. This is useful for stack-like operations. It modifies the original array.

// Example of pop()
let fruits = ["apple", "banana", "cherry"];
let lastFruit = fruits.pop();

console.log(fruits); // Output: ["apple", "banana"]
console.log(lastFruit); // Output: "cherry"
Output:
["apple", "banana"]
"cherry"

Note:pop() changes the array length. It is fast and simple. Use it when you need the last item removed.

Method 2: Using shift()

The shift() method removes the first element. It returns that element too. This is like a queue operation. It shifts all other elements down one index.

// Example of shift()
let colors = ["red", "green", "blue"];
let firstColor = colors.shift();

console.log(colors); // Output: ["green", "blue"]
console.log(firstColor); // Output: "red"
Output:
["green", "blue"]
"red"

Note:shift() is slower than pop() because it re-indexes the array. Use it only when you need the first element.

Method 3: Using splice()

The splice() method is very powerful. It can remove elements from any position. It takes three arguments: start index, delete count, and optional items to add. It returns the removed elements.

// Example of splice()
let numbers = [10, 20, 30, 40, 50];
let removed = numbers.splice(2, 1); // Remove 1 element at index 2

console.log(numbers); // Output: [10, 20, 40, 50]
console.log(removed); // Output: [30]
Output:
[10, 20, 40, 50]
[30]

You can also remove multiple items at once. For example, splice(1, 3) removes three items starting from index 1. This method modifies the original array directly.

Important:splice() is versatile. It is the best choice for removing items at specific positions. For more array techniques, check our JavaScript Array Methods Guide.

Method 4: Using filter()

The filter() method creates a new array. It does not modify the original. It keeps only elements that pass a test. This is perfect for conditional removal.

// Example of filter()
let ages = [15, 22, 18, 30, 12];
let adults = ages.filter(age => age >= 18);

console.log(adults); // Output: [22, 18, 30]
console.log(ages); // Output: [15, 22, 18, 30, 12] (unchanged)
Output:
[22, 18, 30]
[15, 22, 18, 30, 12]

Note:filter() is great when you need to remove items based on a condition. It is also useful for creating subsets. Remember, it returns a new array, so assign it to a variable.

Method 5: Using the delete Operator

The delete operator removes an element from an array. But it leaves an empty slot. This is not ideal for most cases. It does not change the array length.

// Example of delete
let animals = ["cat", "dog", "bird"];
delete animals[1];

console.log(animals); // Output: ["cat", empty, "bird"]
console.log(animals.length); // Output: 3
Output:
["cat", empty, "bird"]
3

Avoid this method unless you have a specific reason. It creates sparse arrays. This can cause unexpected behavior in loops. Use splice() or filter() instead.

Method 6: Removing by Value

Sometimes you do not know the index. You only know the value. You can combine indexOf() and splice() to remove by value.

// Remove by value
let items = ["pen", "book", "eraser", "book"];
let valueToRemove = "book";
let index = items.indexOf(valueToRemove);

if (index !== -1) {
    items.splice(index, 1);
}

console.log(items); // Output: ["pen", "eraser", "book"]
Output:
["pen", "eraser", "book"]

Note: This removes only the first occurrence. To remove all occurrences, use filter() as shown earlier.

Method 7: Removing All Occurrences

To remove every instance of a value, use filter(). This is clean and efficient.

// Remove all occurrences
let scores = [85, 90, 85, 70, 85];
let scoreToRemove = 85;
let filteredScores = scores.filter(score => score !== scoreToRemove);

console.log(filteredScores); // Output: [90, 70]
Output:
[90, 70]

This method is safe and does not mutate the original array. It is ideal for data integrity.

Performance Considerations

Performance matters for large arrays. pop() and shift() are O(1) and O(n) respectively. splice() is O(n) because it shifts elements. filter() is also O(n) but creates a new array. Choose based on your needs.

For small arrays, any method works. For large arrays, avoid shift() and delete. Use filter() for condition-based removal. For index-based removal, splice() is best. Learn more about array handling in our JavaScript Array Variables Guide.

Common Mistakes to Avoid

Beginners often confuse these methods. Here are pitfalls to avoid:

  • Using delete thinking it removes the element completely.
  • Forgetting that splice() modifies the original array.
  • Not reassigning the result of filter().
  • Using pop() when you need to remove from the middle.

Always test your code. Use console.log to verify the output. This builds confidence and skill.

Real-World Example: Managing a Task List

Imagine you have a task list. You want to remove completed tasks. Here is a practical example:

// Task list example
let tasks = [
    { id: 1, name: "Write code", completed: true },
    { id: 2, name: "Test features", completed: false },
    { id: 3, name: "Deploy app", completed: true }
];

// Remove completed tasks using filter
let pendingTasks = tasks.filter(task => !task.completed);

console.log(pendingTasks);
// Output: [{ id: 2, name: "Test features", completed: false }]
Output:
[{ id: 2, name: "Test features", completed: false }]

This is clean and readable. It shows the power of filter() in real scenarios.

Conclusion

Removing items from arrays is a fundamental skill in JavaScript. You now know multiple methods: pop() for the end, shift() for the start, splice() for specific positions, filter() for conditions, and delete for sparse arrays. Each has its place. For most tasks, splice() and filter() are your best friends. Practice these examples to master array removal. Your code will become cleaner and more efficient. Keep coding and exploring. For a deeper dive into array functions, revisit our JavaScript Array Methods Guide.