Skip to main content Accessibility Feedback

Arrays

Array.prototype.splice()

Delete, replace, and add items to an array at specific indexes. The Array.prototype.splice() method accepts three arguments: start, delete, and items.

The first, start, is the index of the item you want to modify in the array. It’s the only required argument.

The second, delete, is the number of items to delete from the array. If you omit this argument, the Array.prototype.splice() method will remove every item from the start index on. If you set it to 0, it won’t remove any items.

Finally, if you want to insert one or more items into the array, you can pass them in as additional arguments.

let sandwiches = ['turkey', 'tuna', 'ham', 'pb&j'];

// Remove "ham" from the array
// It has an index of 2, and we only want to remove 1 item
sandwiches.splice(2, 1);

// Add "italian" between "tuna" and and "ham"
// Our target index is 2, we want to remove 0 items, and add "italian"
sandwiches.splice(2, 0, 'italian');

// Replace "tuna" with "chicken salad"
// It has an index of 1, we want to remove 1 item, and add "chicken salad"
sandwiches.splice(1, 1, 'chicken salad');

You can combine splice() with indexOf() to remove an item by it’s name.

// Remove "pb&j"
sandwiches.splice(sandwiches.indexOf('pb&j'), 1);

Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.


Find this useful? You can support my work by purchasing an annual membership.