Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

splice

splice() changes an array in place by removing, replacing, and/or inserting elements. It returns a new array containing the elements removed.

How it works

Call splice() on an array with a starting index, an optional removal count, and optional replacement elements. It changes the array itself, removes the requested elements, inserts the supplied items at that position, and returns a new array containing what was removed.

Syntax

splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2)
splice(start, deleteCount, item1, item2, /* …, */ itemN)

Parameters

NameTypeDescription
startintegerZero-based position at which changes begin; negative values count from the end.
deleteCountintegerNumber of elements to remove starting at start.
item1, …, itemNanyOne or more values inserted at start after removals.

Returns

A new array containing the elements removed from the original array.

Key points

  • It can remove, replace, or insert elements during one in-place operation.
  • start identifies the first position affected and is converted to an integer.
  • deleteCount controls how many existing entries are removed.
  • Additional arguments become inserted elements at the modification point.
  • The method can operate on array-like objects when they provide suitable indexed properties and a length.
  • Sparse arrays retain their holes through the operation.
  • Using the array length as start with zero removals appends supplied elements.

Common mistakes

  • splice() changes the original array; use toSpliced() when the original must remain unchanged.
  • Replacing a different number of elements can expand or shrink the array.
  • A negative start index addresses positions from the array's end, subject to the documented bounds.

Example: Replace an array element

const months = ["Jan", "March", "April", "June"];
const removed = months.splice(1, 1, "Feb");
console.log(months); // ["Jan", "Feb", "April", "June"]
console.log(removed); // ["March"]

Replaces one element at index 1, mutates the original array, and returns the removed elements.

When to use it

Delete elements

Remove a contiguous section when its index and length are known.

Insert elements

Insert values at a chosen position without deleting existing entries.

Replace elements

Swap existing values while preserving the array's position structure.

Interview questions

QWhat does Array.prototype.splice() do?

splice() mutates the receiver and returns an array containing the elements removed. Its arguments determine where changes begin, how many elements are removed, and which new elements are inserted.

Follow-up: How would you perform the same operation immutably? Use toSpliced(), which is the copying version of splice().

QHow is the start parameter interpreted?

start is the zero-based position where modification begins. A negative start counts backward from the end, while a value beyond the array is handled as the effective end position.

Follow-up: What happens when start is negative but outside the array range? The documented conversion rules clamp it to the beginning of the array.

QHow do deleteCount and inserted items interact?

deleteCount specifies the number of existing elements removed from start. item1 through itemN are inserted at that same position, and the returned array contains the removed elements.

Follow-up: How can splice() insert without removing? Pass a deleteCount of 0 and provide the elements to insert.

  • Array.prototype.toSpliced()
  • Array.prototype.push()
  • Array.prototype.unshift()

Source: Array.prototype.splice () - JavaScript | MDN. Checked 2026-10-15.