Remove items from JavaScript array

If you have an array object in JavaScript and want to remove items from it, you can make use of the indexOf() and splice() methods. method.

  1. Say you have an array of fruits: var fruits = ['orange', 'mango', 'banana', 'grapes', 'plum'];
  2. You can get the index of any element/item as: var idx = fruits.indexOf('banana');
  3. Now you can use this index with slice() to remove the item banana from the array: fruits.splice(idx, 1);

Note: if the indexOf of the object is not found then you would get -1, so you can wrap the slice method with an if condition to check that.

Let's see an example JavaScript code snippet:

//Step 1: Note we have 5 elements/items in the array fruits
var fruits = ['orange', 'mango', 'banana', 'grapes', 'plum'];

//Let's print the array in console
console.log("Before removal: "+fruits);

//Step 2: Now lets get index of banana element that we want to remove
const idx = fruits.indexOf('banana');

//Step 3: Lets slice the element out
if(idx > -1)
fruits.splice(idx, 1);

//Step 4: Let's print the array again in the console to see the result.
console.log("After removal: "+fruits);

> "Before removal: orange,mango,banana,grapes,plum"
> "After removal: orange,mango,grapes,plum"

jsFiddle Example:


  • This was helpful. Thank you very much.
    abidU 21 Aug 2020 12:08:49 GMT
  • Further comments disabled!
Try Out Code2care Dev Tools:


Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR -