Guvi-blog-logo

How To Remove A Specific Item From An Array In JavaScript

How Can I Remove A Specific Item From An Array In JavaScript

The array plays an important role not only in programming languages like Python, Java, and more but also in JavaScript. It allows the user to store several values within a single variable. Sometimes, the user needs to delete the element from the array. The reason might be that they need to use the array for different purposes and something else. So how can I remove a specific item from an array? 

Well, there are different approaches to do this. Do you have an idea how to do this? Don’t worry; we have explained those approaches in the below section. 

So without discussing more, let’s start with the details.

How can I remove a specific item from an array?

As already mentioned there are different approaches to do this. We have explained the three most used methods. And these are:

  • .splice( )
  • .indexOf( ) and .splice( )
  • Traditional method (using the For Loop)

Want to master JavaScript? Get started here and ace your tech career!

Let’s find out how these methods work with useful examples.

1) .splice method

[This will be used if you know the index of the item you want to remove.]

The .splice() method is one of the built-in methods in JS for the array objects. Using it, you can change the content of the array by removing or replacing the items with new ones. 

With the help of the .splice() method, you can easily modify the original array. If you want to get the removed items like a new array, you can do that using .splice().

Let’s understand how to use .splice()

[ Note: You might have heard that an array in JS can store any number, string, boolean, or null. Apart from this, the array can store the combination of these data types. That is why I have taken the example of string data type.]

Syntax: Array.splice(start, removeCount, Item1, Item2, …, ItemN)

Suppose you have two month’s names and two week day’s names as:

January, February, Monday, Tuesday

And you want to separate the week day’s names by removing the month’s names. Then, write the code as:

Output:

[“January”, “February”]

In this code, the splice starts removing all the items from the 2nd index value. And it provided the output of the current items stored in an array.

Want to learn JavaScript from scratch? Ace into Web Development from there!

[NOTE: The indexing in JS starts from 0 and count as 1,2,…n]

Now the question is “how can I remove a specific item from an array” using the .splice() method. Let’s understand it with a shortcode:

Output:

[“January”, “Tuesday”]

[“February”, “Monday”]

You can see that we have used .splice(1,2). This means we want to remove 2 items that will start from 1 index value.

After this, we have got the output of the current array as names. And removed items of the array as removed.

This is how to remove a specific item in an array using the .splice() method. (Only if you have the index value of the item you want to remove).

2) indexOf () and .splice() method

[This will be used if you do not know the index of the item you want to remove.]

Sometimes, the user doesn’t know the exact index of the item. In that case, he/she can use the .indexOf() along with the .splice() method.

The indexOf() will always return the index of the item. But if the item is not present in the array, then it will return -1. Let’s take an example of it first:

Output:

-1

Output:

2

As you can see “Wednesday” is not in the names1 array; therefore, the output is -1. On the other hand, “Monday” is present in the names2 array at the 3 positions with the 2nd index value, the output is 2.

Now, how to remove a specific item in an array using the .indexOf() and .splice() method. 

Let’s take an example of it:

Output:

[“February”, “Monday”]

[“January”, “Tuesday”]

In the above code, the first indexOf checks the index of the item “February” of the names array. That is stored in the days. Then, the splice takes the index value from the days and removes 2 items from that index. Further, the result will be stored in removed

This is how can I remove a specific item from an array without even knowing the index of the item.

Master Full Stack Development and Start building responsive websites!
Key point:
Can I remove two or more similar items from an array using the .splice() method?
No, you can’t!!
It is not possible to remove two or more similar items from an array. Let’s take an example of it:
let names = [“January”, “February”, “Monday”, “February”, “Tuesday”];let removed = names.splice(1, 1);console.log(removed);console.log(names);

Output:
[“February”][“January”, “Monday”, “February”, “Tuesday”]
Here, February is at index values 1 and 4, but it has removed the 1 index valued item. Then it terminates the processing (once it gets the 1st indexed item). So, the .splice() method is not suitable for removing two or more similar items.

3) Traditional method (using the For Loop)

This is an alternate, but effective method that is used for removing the item from an array. So, how can I remove a specific item from an array in JS using a for loop method? 

Let’s take an example of it for a better understanding:

Output:

[“green”, “black”]

Here, we have defined the colors variable array first. Then, use a for loop, where variable j has the same length as that of the colors array that starts from index 0 to the final length item. 

Now, the if statement will execute when variable j gets the item red. After this, .splice() will remove the j variable that is red and keep removing items till 2 elements get removed. Finally, we get the current elements of the colors array as [“green”, “black”].

You can see that the colors array has two elements. This is how can I remove a specific item from an array using a for a loop.

Final Thoughts!!

Various JavaScript learners or beginners might be facing difficulties with their JavaScript codes. And removing the element from an array can be one of those difficulties. That is why I have explained the most used or preferred approaches for “how can I remove a specific item from an array”. 

Use and practice the above-mentioned methods to understand the functions’ concepts effectively. I think you will definitely run your programs without facing any issues. But in case you have any issues; let me know about them. 

I will provide you more examples for a better understanding. So stay in touch with us to learn more about JavaScript concepts. Keep practicing to get expertise in the JavaScript programs. 

Let’s test what you have studied and understood from this blog!

  1. Which of these methods use for deleting array items in JavaScript.

(i) splice

(ii) filter

(iii) shift

(iv) pop

(A) i, iii & iv only

(B) i, & iv only

(C) ii, & iii only

(D) All i, ii, iii & iv 

Correct Answer: (D) 
splice – Delete a specific element from an array with the particular index filter Enables the user to delete elements from the Array programmatically shift Delete the element from the Array’s beginning pop – Delete the element from the Array’s End 
  1. What will be the output of the below code?

var arr=[“Mon”,”Tue”,”Sat”,”Sun”,”Thurs”,”Fri”];  

var result=arr.splice(2,2,”Wed”)  

console.log(arr)

(A) [“Sat”, “Sun”]

(B) [“Mon”,”Tue”,”Sat”,”Sun”,”Thurs”,”Fri”]

(C) [“Mon”, “Tue”, “Wed”, “Thurs”, “Fri”]

(D) None of these

Correct Answer: (C)
Splice has removed the 2 elements starting from the 2nd index and added “Wed” at the 2nd index.
  1. What will be the output of the following code?

var arr=[“Mon”,”Tues”,”Sat”,”Sun”,”Thurs”,”Fri”];  

var result=arr.splice(2);  

console.log(arr)

(A) [“Sat”,”Sun”,”Thurs”,”Fri”]

(B) [“Mon”, “Tues”]

(C) [“Sat”]

(D) None of these

Correct Answer: (B)
Splice has removed all the elements from the 2nd Index.
  1. What will be the output of the following program?

var j = [“Alice”, “Tom”, “Alice”];

j.indexOf(“Alice”);

(A) 0

(B) 2

(C) 0, 2 

(D) None of these

Correct Answer: (A)
As you can see that at 0 (zero) indexes the “Alice” name is found, it will show its index and terminate without executing a similar item further.
  1. Can splice remove various similar items from the array in JavaScript?

(A) Yes

(B) No

Correct Answer: (B)
Splice can only remove a single similar item from the given array.

Contact Form

By clicking 'Submit' you Agree to Guvi Terms & Conditions.

Our Learners Work at

Our Popular Course

Share this post

Author Bio

GUVI Geek
GUVI Geek

Our Live Classes

Learn Javascript, HTML, CSS, Java, Data Structure, MongoDB & more
Learn Python, Machine Learning, NLP, Tableau, PowerBI & more
Learn Selenium, Python, Java, Jenkins, Jmeter, API Testing & more
Learn Networking, Security Testing, IAM, Access Management & more

Hey wait, Don’t miss New Updates from GUVI!

Get Your Course Now

Related Articles