# .toSpliced()

> The `.toSpliced()` JS method returns a shallow copy of an array with elements removed, replaced or added at a given index, leaving the original intact.

## How do I remove an item from an array immutably with toSpliced()?

The method that returns a new array by removing, replacing or adding elements based on the original. It starts at a specified position and can remove a certain number of elements while optionally replacing them with new elements. The original array remains unchanged.

- Topic: Array
- Type: method
- Browser support: Chrome 110+, Chrome Android 110+, Edge 110+, Firefox 115+, Firefox Android 115+, Safari 16+, Safari iOS 16+

## Example

Wrong:

```js
function remove(index) {
  setTodos(todos.splice(index, 1));
}
```

Right:

```js
function remove(index) {
  setTodos(todos.toSpliced(index, 1));
}
```

`splice()` mutates state and returns the removed item; `toSpliced()` returns a new array without it.

## Common mistakes

- Expecting `toSpliced()` to return the removed items, as `splice()` does.

## Key points

- The code uses the array returned by `toSpliced()`, not the removed items as with `splice()`.
- The index is checked for `-1` before calling `toSpliced(i, 1)`.

## Interview questions

### How do I insert into an array immutably?

Use a delete count of `0`: `arr.toSpliced(index, 0, item)` returns a copy with `item` inserted.

### Should I use `toSpliced()` or `filter()` to remove items?

Use `toSpliced()` to remove by position and `filter()` to remove by condition.

## Related cards

- [.splice()](https://wonderium.dev/js/array-splice.md)
- [.with()](https://wonderium.dev/js/array-with.md)
- [.toSorted()](https://wonderium.dev/js/array-to-sorted.md)
- [Array.slice()](https://wonderium.dev/js/array-slice.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/array-to-spliced
- Last updated: 2026-11-25
