JavaScript slice vs splice: The Difference Explained
Contents
- The difference in one example
- slice vs splice comparison
- slice(): copy part of an array
- slice() makes a shallow copy
- splice(): remove, replace or insert
- Remove an item by value with splice()
- Gotcha: splice() inside a forward loop
- Gotcha: an undefined deleteCount removes nothing
- toSpliced(): splice without changing the array
- String slice() works the same way
- FAQ
Short answer: slice() returns a copy of part of an array and leaves the original alone. splice() changes the original array in place: it removes, replaces or inserts items, and returns the items it removed.
The names are one letter apart, but they do opposite things to your data. Here’s how each one works, the arguments that trip people up, and when to use toSpliced() instead.
The difference in one example
Same array, same numbers, very different results. After slice() the array is untouched. After splice() two items are gone for good.
const nums = [1, 2, 3, 4, 5];
nums.slice(1, 3); // [2, 3]
nums; // [1, 2, 3, 4, 5] unchanged
nums.splice(1, 2); // [2, 3]
nums; // [1, 4, 5] changed
Notice that the second argument means different things. In slice(1, 3) the 3 is an end index (not included). In splice(1, 2) the 2 is a count of items to remove. Both calls happen to return [2, 3] here, which is exactly why the two get mixed up.
slice vs splice comparison
slice(start, end) |
splice(start, deleteCount, ...items) |
|
|---|---|---|
| Changes the original? | No | Yes |
| Second argument | End index (exclusive) | How many items to remove |
| Returns | New array with the selected items | Array of the removed items |
| Can add items? | No | Yes, from the third argument on |
| Negative indexes | Yes, for start and end | Yes, for start |
| Works on strings? | Yes, String.prototype.slice |
No |
| Non-mutating version | It already is one | toSpliced() |
slice(): copy part of an array
slice(start, end) copies items from start up to, but not including, end. Leave out end to go to the end of the array. Negative numbers count from the end, so -2 means “second to last”. If start is past end, you get an empty array, not an error.
const letters = ['a', 'b', 'c', 'd', 'e'];
letters.slice(2); // ['c', 'd', 'e']
letters.slice(-2); // ['d', 'e']
letters.slice(1, -1); // ['b', 'c', 'd']
letters.slice(3, 1); // []
letters.slice(); // ['a', 'b', 'c', 'd', 'e'] a copy
slice() makes a shallow copy
arr.slice() with no arguments is a classic way to copy an array before sorting or reversing it. The copy is shallow: the new array holds the same object references, so changing an object inside the copy changes it in the original too.
const users = [{ name: 'Ana' }];
const copy = users.slice();
copy[0].name = 'Ben';
users[0].name; // 'Ben' same object in both arrays
splice(): remove, replace or insert
splice(start, deleteCount, ...items) starts at start, removes deleteCount items, then inserts any extra arguments in their place. It always returns an array of what it removed, which is empty when you only insert.
// Remove 2 items from index 1
const a = ['a', 'b', 'c', 'd', 'e'];
a.splice(1, 2); // ['b', 'c']
a; // ['a', 'd', 'e']
// Replace 2 items with 1
const b = ['a', 'b', 'c', 'd', 'e'];
b.splice(1, 2, 'X'); // ['b', 'c']
b; // ['a', 'X', 'd', 'e']
// Insert without removing (deleteCount 0)
const c = ['a', 'b', 'c'];
c.splice(1, 0, 'x', 'y'); // []
c; // ['a', 'x', 'y', 'b', 'c']
// Remove everything from index -2 on
const d = ['a', 'b', 'c', 'd', 'e'];
d.splice(-2); // ['d', 'e']
d; // ['a', 'b', 'c']
Remove an item by value with splice()
The most common use of splice(): find the index, then remove one item. Always check for -1 first. indexOf returns -1 when the item isn’t there, and splice(-1, 1) would quietly remove the last item instead.
const items = ['a', 'b', 'c', 'd'];
const i = items.indexOf('c');
if (i !== -1) {
items.splice(i, 1);
}
items; // ['a', 'b', 'd']
Gotcha: splice() inside a forward loop
Removing items while looping forward skips elements, because everything after the removed item shifts one index to the left. Here the 4 is never checked. Loop backwards, or use filter() to build a new array instead.
const nums = [1, 2, 4, 6, 5];
for (let i = 0; i < nums.length; i++) {
if (nums[i] % 2 === 0) nums.splice(i, 1);
}
nums; // [1, 4, 5] the 4 survived
const fixed = [1, 2, 4, 6, 5];
for (let i = fixed.length - 1; i >= 0; i--) {
if (fixed[i] % 2 === 0) fixed.splice(i, 1);
}
fixed; // [1, 5]
Gotcha: an undefined deleteCount removes nothing
Leaving out deleteCount removes everything from start to the end. Passing undefined explicitly is different: it’s treated as 0, so nothing is removed. This bites when the count comes from a variable that wasn’t set.
['a', 'b', 'c'].splice(1); // ['b', 'c'] removed to the end
['a', 'b', 'c'].splice(1, undefined); // [] removed nothing
toSpliced(): splice without changing the array
toSpliced() takes the same arguments as splice() but returns a new array with the change applied and leaves the original alone. That’s what you want in React or Vue state, where mutating the existing array causes bugs. It’s supported in Chrome and Edge 110+, Firefox 115+, Safari 16+ and Node.js 20+ (see MDN). Note the return value: the new array, not the removed items.
const letters = ['a', 'b', 'c', 'd', 'e'];
const updated = letters.toSpliced(1, 2, 'X');
updated; // ['a', 'X', 'd', 'e']
letters; // ['a', 'b', 'c', 'd', 'e'] unchanged
String slice() works the same way
Strings have slice() with the same start/end rules and negative indexes. They don’t have splice(), because strings can’t be changed in place. To “splice” a string, slice it and join the parts.
'JavaScript'.slice(0, 4); // 'Java'
'JavaScript'.slice(-6); // 'Script'
typeof 'x'.splice; // 'undefined'
const s = 'Hello world';
s.slice(0, 5) + ',' + s.slice(5); // 'Hello, world'
Both methods sit alongside map(), filter() and find() in the everyday toolbox; if you want a refresher on those, see 10 JavaScript array methods to simplify your code. For more small tricks like these, there’s a collection of JavaScript tips. To merge nested arrays into one, use flat().
FAQ
Does slice() change the original array?
No. slice() always returns a new array and never touches the original. splice() is the one that changes it.
What does splice() return?
An array of the removed items. If you only insert (with a deleteCount of 0), it returns an empty array. It does not return the updated array.
How do I remove an element from an array without mutating it?
Use toSpliced(index, 1), or filter() when you’re removing by value: arr.filter(x => x !== value). Both return a new array.
Is the end index in slice() included?
No. slice(1, 3) returns the items at index 1 and 2. A handy check: the result length is end - start when both are in range.
Can I use slice() on strings?
Yes. String.prototype.slice() works like the array version, negative indexes included. Strings have no splice().