How can I remove a specific item from an array in JavaScript?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

How Can I Remove a Specific Item from an Array in JavaScript?

Removing a specific item from an array in JavaScript can be done using various methods. Here are several ways to achieve this, each with its own use cases and advantages.

Using 'splice' Method

The splice method can be used to remove an item from an array by its index. This method modifies the original array.

Example:

let array = [1, 2, 3, 4, 5]; let index = array.indexOf(3); // Find the index of the item to be removed if (index !== -1) { array.splice(index, 1); // Remove the item at the found index } console.log(array); // Output: [1, 2, 4, 5]

Using 'filter' Method

The filter method creates a new array with all elements that pass the test implemented by the provided function. This method does not modify the original array.

Example:

let array = [1, 2, 3, 4, 5]; let valueToRemove = 3; array = array.filter(item => item !== valueToRemove); console.log(array); // Output: [1, 2, 4, 5]

Using 'slice' Method with indexOf

You can use the slice method in combination with indexOf to create a new array without the specific item. This method does not modify the original array.

Example:

let array = [1, 2, 3, 4, 5]; let valueToRemove = 3; let index = array.indexOf(valueToRemove); if (index !== -1) { array = array.slice(0, index).concat(array.slice(index + 1)); } console.log(array); // Output: [1, 2, 4, 5]

Using 'for' Loop

You can use a for loop to iterate through the array and construct a new array excluding the specific item. This method does not modify the original array.

Example:

let array = [1, 2, 3, 4, 5]; let valueToRemove = 3; let newArray = []; for (let i = 0; i < array.length; i++) { if (array[i] !== valueToRemove) { newArray.push(array[i]); } } array = newArray; console.log(array); // Output: [1, 2, 4, 5]

Using 'reduce' Method

The reduce method can be used to accumulate values into a new array, excluding the specific item. This method does not modify the original array.

Example:

let array = [1, 2, 3, 4, 5]; let valueToRemove = 3; array = array.reduce((acc, item) => { if (item !== valueToRemove) { acc.push(item); } return acc; }, []); console.log(array); // Output: [1, 2, 4, 5]

Summary

  • splice Method: Directly removes the item by index and modifies the original array.
  • filter Method: Creates a new array excluding the specific item, without modifying the original array.
  • slice Method with indexOf: Combines slicing and concatenation to create a new array, without modifying the original array.
  • for Loop: Constructs a new array by iterating and excluding the specific item, without modifying the original array.
  • reduce Method: Accumulates values into a new array, excluding the specific item, without modifying the original array.

Choose the method that best fits your needs based on whether you want to modify the original array or create a new one. For a more comprehensive understanding of array manipulation and other JavaScript concepts, consider exploring Grokking the Coding Interview on DesignGurus.io, which provides in-depth explanations and examples to help you master coding interview techniques.

TAGS
System Design Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Do you need to know sorting algorithms for coding interviews?
Refining solutions by applying complexity-reducing techniques
Is ServiceNow a good career option?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.