Skip to main content Accessibility Feedback

How to remove an item from an array with vanilla JS

The Array.splice() method takes an array and returns a new one with just the spliced elements.

For example, let’s say I had an array of wizards.

var wizards = ['Neville', 'Hermione', 'Harry Potter', 'Dumbledore'];

If I wanted to remove Harry Potter (obviously making the whole set stronger), I would call the splice() method on wizards. The first argument is the index of the item to remove, and the second is how many items to remove starting at that spot.

(Remember, array indexes start at 0, so Harry Potter has an index of 2.)

var harry = wizards.splice(2, 1);

Now, harry is an array with just Harry Potter in it, while wizards contains Neville, Hermione, and Dumbledore.

Here’s a demo.

What if you don’t know the index of the item?

You can use indexOf() to get it.

// Find Harry Potter
var index = wizards.indexOf('Harry Potter');

// If he's in the array, remove him
if (index > -1) {
	wizards.splice(index, 1);
}

This works in all modern browsers and back to at least IE6.