Skip to main content

The toSpliced() method

toSpliced() is the new non-mutating counterpart of the splice() method, added to the standard in ECMAScript 2023 (ES14). It removes, adds or replaces elements, but instead of changing the original array it returns a new copy with those changes applied.

Theory

TL;DR

  • toSpliced() does the same job as splice(): it removes, inserts and replaces elements.
  • The original array stays unchanged, the method returns a new array.
  • The syntax is identical: array.toSpliced(start, deleteCount, ...items).
  • It returns the whole new array, not the removed elements the way splice() does.
  • Added in ECMAScript 2023, together with toSorted(), toReversed() and with().
  • Used wherever immutability matters: React, Redux, pure functions.

Quick example

javascript
const arr = [1, 2, 3, 4, 5]; // remove 2 elements starting at index 1 const newArr = arr.toSpliced(1, 2); console.log(newArr); // [1, 4, 5] console.log(arr); // [1, 2, 3, 4, 5], the original is untouched

Syntax and parameters

javascript
const newArray = array.toSpliced(start, deleteCount, ...items);
ParameterDescription
startThe index at which the changes begin
deleteCountHow many elements to remove
...itemsThe elements to insert in that place (optional)

A negative start counts from the end of the array, exactly as in splice(). If deleteCount is omitted, every element from start to the end is removed.

Removing elements

javascript
const arr = [1, 2, 3, 4, 5]; const newArr = arr.toSpliced(1, 2); // remove 2 elements starting at index 1 console.log(newArr); // [1, 4, 5] console.log(arr); // [1, 2, 3, 4, 5], the original is untouched

What happened:

  • [2, 3] was removed;
  • the new array [1, 4, 5] came back;
  • the source array stayed intact.

Adding and replacing elements

Adding, when deleteCount is zero:

javascript
const arr = [1, 2, 3]; const newArr = arr.toSpliced(1, 0, 'a', 'b'); console.log(newArr); // [1, 'a', 'b', 2, 3] console.log(arr); // [1, 2, 3]

Starting at index 1 we remove nothing (deleteCount = 0), insert 'a' and 'b', and get a new array back while the original does not mutate.

Replacing, when we remove and insert at the same time:

javascript
const arr = [1, 2, 3, 4]; const newArr = arr.toSpliced(1, 2, 'x', 'y'); console.log(newArr); // [1, 'x', 'y', 4] console.log(arr); // [1, 2, 3, 4]

Here [2, 3] was removed and 'x', 'y' were inserted in their place.

Comparison with splice()

javascript
const arr = [1, 2, 3, 4]; const mutated = arr.splice(1, 2, 'x', 'y'); console.log(arr); // [1, 'x', 'y', 4], the source array was changed console.log(mutated); // [2, 3], it returns the removed elements const newArr = [1, 2, 3, 4].toSpliced(1, 2, 'x', 'y'); console.log(newArr); // [1, 'x', 'y', 4], a new array

The main difference is not only the mutation, but also what the method returns:

splice()toSpliced()
Changes the source arrayYesNo
ReturnsThe removed elementsThe whole new array
ChainableNoYes
Introduced inES3ES2023

Because toSpliced() returns an array, you can put it in a chain:

javascript
const result = [5, 1, 4, 2] .toSpliced(0, 1) // [1, 4, 2] .toSorted(); // [1, 2, 4]
New methodOld counterpartWhat it doesMutates the array
toReversed()reverse()Reverses the orderNo
toSorted()sort()SortsNo
toSpliced()splice()Removes, inserts, replacesNo
with()arr[index] = valueChanges an element by indexNo

All four return a shallow copy: nested objects in the new array are still the very same references as in the old one.

When to use it

  • When you need to change an array without side effects: in React, Redux or any immutable code.
  • When the original data must be preserved for comparison or for undo.
  • When state is compared by reference: a new array guarantees the component re-renders.

The method in brief:

QuestionAnswer
What it doesBuilds a new version of the array with elements removed, added or replaced
Changes the source arrayNo
ReturnsA new array
Supports insertion and replacementYes
Added inECMAScript 2023
Example[1, 2, 3].toSpliced(1, 1, 'X') -> [1, 'X', 3]

Common mistakes

  • Expecting the method to return the removed elements. splice() returns what was removed, toSpliced() returns the new array. Code like const removed = arr.toSpliced(1, 2) will not give you what you expect.
  • Calling toSpliced() and throwing the result away. arr.toSpliced(1, 2); does nothing useful: the original is not changed and the new array is discarded immediately.
  • Assuming the copy is deep. It is a shallow copy, so mutating a nested object still affects both arrays.
  • Forgetting about environment support. The method exists in modern browsers and in Node.js from version 20; for older targets you need a polyfill or a build with the right target.
  • Confusing toSpliced() with slice(). slice(start, end) only cuts out a range and inserts nothing, while toSpliced() takes a deleteCount and a list of new elements.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.