Javascript Splice — A Developer's Guide

February 11, 2026

const colors = ['red', 'green', 'blue', 'yellow', 'purple'];
colors.splice(2, 1);
console.log(colors);
[ 'red', 'green', 'yellow', 'purple' ]

Ah, the sweet taste of victory when you finally figure out how to remove that one pesky element from an array in JavaScript! I've been there, folks — debugging at 2am, wondering why `splice` just won't cooperate. But fear not, dear developers, for today we're going to conquer the `javascript splice` method once and for all.

What is `splice`, anyway?

So, you know how sometimes you need to remove or add elements to an array? That's where `splice` comes in — it's like a Swiss Army knife for array manipulation. The `splice` method changes the contents of an array by removing or replacing existing elements and/or adding new elements.

The `splice` syntax

The `splice` method takes three arguments: `start`, `deleteCount`, and `item1`, `item2`, ... (optional). Here's what they do:


const numbers = [1, 2, 3, 4, 5];
numbers.splice(1, 2, 'a', 'b');
console.log(numbers);
[ 1, 'a', 'b', 4, 5 ]

See how we removed two elements from the array and added two new ones? This is where things can get a bit tricky — if you don't specify any elements to add, `splice` will simply remove the specified number of elements from the array.

Adding elements with `splice`

So far, we've only seen how to remove elements with `splice`. But what if we want to add some new elements to our array? Easy peasy!


const fruits = ['apple', 'banana'];
fruits.splice(1, 0, 'orange');
console.log(fruits);
[ 'apple', 'orange', 'banana' ]

Notice how we passed `0` as the second argument? This tells `splice` not to remove any elements from the array. Instead, it simply adds the new element at the specified index.

Replacing elements with `splice`

Sometimes you don't want to just add or remove elements — you want to replace them entirely. That's where things can get a bit hairy with `splice`. But don't worry, I've got your back!


const colors = ['red', 'green', 'blue'];
colors.splice(1, 1, 'yellow');
console.log(colors);
[ 'red', 'yellow', 'blue' ]

See how we replaced the second element with a new one? This is where it's easy to get confused with `splice`. Just remember that if you specify a non-zero value for the second argument (`deleteCount`), those many elements will be removed from the array before adding any new ones.

javascript splice meme

Edge cases with `splice`

As with any JavaScript method, there are some edge cases to watch out for when using `splice`. Here are a few:


const numbers = [1, 2];
numbers.splice(10); // Whoops!
console.log(numbers);
[ ]

If you try to splice an index that doesn't exist in your array... well... weird things happen. Don't try this at home!

`javascript splice` vs other methods: which should I use?

While we've only talked about using splice so far... there are other ways you can manipulate arrays in JavaScript too! These include shift/unshift/pop/push/concat/join/slice etc...