---
title: "Remove a specific item from an array in JavaScript."  
description: "Remove a specific item from an array in JavaScript."  
author: "Steilla Mitchel"  
published: 2023-07-20  
updated: 2023-07-21  
canonical: https://www.mindstick.com/forum/159191/remove-a-specific-item-from-an-array-in-javascript  
category: "javascript"  
tags: ["javascript", "array list"]  
reading_time: 2 minutes  

---

# Remove a specific item from an array in JavaScript.

How can I [remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) a specific [item from an array](https://www.mindstick.com/forum/158052/how-can-i-remove-a-specific-item-from-an-array-in-javascript) in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Aryan Kumar

There are a few ways to remove a specific [item](https://www.mindstick.com/forum/156564/remove-item-in-an-array-in-javascript) from an [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) in JavaScript.

**Method 1: Using the** `indexOf()` **method**

The `indexOf()` method can be used to find the index of an item in an array. Once you have the index of the item you want to remove, you can use the `splice()` method to remove it from the array.

The `splice()` method takes three arguments:

- The index of the item you want to remove
- The number of items you want to remove
- A new array of items to replace the removed items

For example, the following code removes the item with the index 1 from the array `myArray`:

```plaintext
const myArray = [1, 2, 3, 4];

const index = myArray.indexOf(2);

myArray.splice(index, 1);

console.log(myArray); // [1, 3, 4]
```

**Method 2: Using the** `for` **loop**

The `for` loop can be used to iterate through the array and remove the item you want to remove.

The `for` loop takes three arguments:

- The starting index
- The ending index
- A function to be executed for each item in the array

For example, the following code removes the item with the value `2` from the array `myArray`:

```plaintext
const myArray = [1, 2, 3, 4];

for (let i = 0; i < myArray.length; i++) {
  if (myArray[i] === 2) {
    myArray.splice(i, 1);
    break;
  }
}

console.log(myArray); // [1, 3, 4]
```

**Method 3: Using the** `while` **loop**

The `while` loop can be used to iterate through the array and remove the item you want to remove.

The `while` loop takes two arguments:

- A condition that must be true for the loop to continue
- A function to be executed for each item in the array

For example, the following code removes the item with the value `2` from the array `myArray`:

```plaintext
const myArray = [1, 2, 3, 4];

var i = 0;

while (i < myArray.length) {
  if (myArray[i] === 2) {
    myArray.splice(i, 1);
    break;
  }
  i++;
}

console.log(myArray); // [1, 3, 4]
```


---

Original Source: https://www.mindstick.com/forum/159191/remove-a-specific-item-from-an-array-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
