# .splice()

> The `.splice()` JS method changes an array by removing, replacing or adding elements from a given index for a given count, modifying the original array.

## How do I remove an item at a specific index with splice() in JavaScript?

The method that modifies the contents of an array by removing, replacing or adding elements. It starts at a specified position and can remove a certain number of elements, while optionally replacing them with new elements or inserting new elements at that position.

- Topic: Array
- Type: method
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1+, Firefox Android 4+, Safari 1+, Safari iOS 1+

## Example

Wrong:

```js
const i = list.indexOf(item);
list.splice(i, 1);
```

Right:

```js
const i = list.indexOf(item);
if (i !== -1) list.splice(i, 1);
```

When the item is missing, `indexOf()` returns -1 and `splice(-1, 1)` silently removes the last element.

## Common mistakes

- `splice(-1, 1)` removes the last item when `indexOf()` returned `-1`.
- Calling `splice()` in a forward loop and skipping the next element.

## Key points

- The delete count is passed; `splice(i)` removes everything from `i` to the end.
- The code checks that `indexOf()` is not `-1` before calling `splice()`.
- Immutable updates use `toSpliced()` or `filter()` instead of `splice()`.

## Interview questions

### What does `splice()` return?

An array of the removed elements, or an empty array if nothing was removed.

### How do I insert an element without removing anything?

Pass `0` as the delete count: `arr.splice(index, 0, newItem)`.

## Related cards

- [Array.slice()](https://wonderium.dev/js/array-slice.md)
- [.toSpliced()](https://wonderium.dev/js/array-to-spliced.md)
- [Array.indexOf()](https://wonderium.dev/js/array-index-of.md)
- [Array.filter()](https://wonderium.dev/js/array-filter.md)

---

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