References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript array.splice() method

Method JavaScript All modern browsers Updated
Quick answer

The splice() method changes an array in place — it can remove, replace or insert elements at any position. arr.splice(1, 2) removes two elements starting at index 1; arr.splice(1, 0, "x") inserts "x" without removing anything. It returns an array of the removed elements, and unlike slice(), it mutates the original.

Overview

splice() is the array surgery tool: it can cut elements out, drop new ones in, or do both at once, all at a position you choose. The first argument is where to start, the second is how many elements to remove, and anything after that is inserted at that spot. So splice(2, 1) deletes one element at index 2, while splice(2, 0, "new") inserts "new" there without deleting.

It returns an array of whatever it removed (empty if nothing was), which is occasionally useful. But the main event is the side effect: splice() mutates the original array, shifting later elements to fill or make room. That makes it perfect for editing a list in place — removing the item a user deleted, inserting one at a sorted position.

The constant confusion is with slice(). They sound alike but do opposite things: slice() copies and leaves the original alone; splice() rewrites the original. If you wanted a copy and reached for splice, you'll wonder why your source array changed. When you need a removal by value, find the index with indexOf() first.

Syntax

const removed = array.splice(start)
const removed = array.splice(start, deleteCount)
const removed = array.splice(start, deleteCount, item1, item2, /* ... */)

Parameters

The array.splice() method accepts the following parameters.

Parameter Description
start Index at which to begin changing the array. Negative counts from the end.
deleteCount Optional. How many elements to remove from start. Use 0 to insert without removing.
item1 ... itemN Optional. Elements to insert at start, after any removal.

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const days = ['Mon', 'Tue', 'Thu', 'Fri'];

  // insert 'Wed' at index 2 without removing anything
  days.splice(2, 0, 'Wed');

  document.getElementById('out').textContent = days.join(', ');
  // Mon, Tue, Wed, Thu, Fri
</script>

Best practices

  • Use splice() when you want to edit an array in place; use slice() when you want a copy.
  • To insert without removing, pass 0 as the delete count: arr.splice(i, 0, item).
  • To remove an item by value, get its index with indexOf() first, then splice that index.
  • Capture the return value if you need what was removed — it's an array of the deleted elements.

Frequently asked questions

What is the difference between splice() and slice()?
splice() mutates the array (removing/inserting) and returns the removed elements; slice() returns a copy of part of the array and changes nothing. Splice edits, slice copies.
How do I remove an element at a specific index?
Use arr.splice(index, 1) to remove one element at that position.
How do I insert an element at a position without deleting?
Pass 0 as the delete count: arr.splice(index, 0, newItem).
What does splice() return?
An array of the elements it removed — empty if it only inserted.