slice vs splice in JavaScript: what is the difference?
BeginnerJavaScript interview questionBy UIRound, updated 17 November 2026
Short answer
slice(start, end) returns a new array holding a copy of part of the original and never changes the original. splice(start, deleteCount, ...items) changes the array in place: it removes deleteCount items from start, inserts any new items there, and returns an array of the removed items. If you need splice's edits without the mutation, use toSpliced().
Side by side
slice(start, end) |
splice(start, deleteCount, ...items) |
|
|---|---|---|
| Changes the original array | No | Yes |
| Returns | A new array with the selected items | An array of the removed items (often []) |
| Second argument | End index, not included | How many items to remove |
| Can insert items | No | Yes, any arguments after the second |
| Negative indexes | Yes, for start and end |
Yes, for start |
| No arguments | Copies the whole array | Does nothing, returns [] |
| Exists on strings | Yes, "hello".slice(1, 3) |
No, strings cannot be changed |
A useful memory hook: splice has an extra "p", and it is the one that performs surgery on the array.
slice: copy a range
const letters = ["a", "b", "c", "d", "e"];
letters.slice(1, 3); // ["b", "c"] (index 1 up to, not including, 3)
letters.slice(2); // ["c", "d", "e"] (to the end)
letters.slice(); // ["a", "b", "c", "d", "e"] (a shallow copy)
letters.slice(3, 1); // [] (start is past end)
console.log(letters); // ["a", "b", "c", "d", "e"] (unchanged)The copy is shallow. The new array is a different array, but if the items are objects, both arrays hold the same objects. Changing copy[0].name also changes original[0].name. The shallow vs deep copy answer covers when that matters.
splice: remove, insert or replace in place
const nums = [1, 2, 3, 4, 5];
const removed = nums.splice(1, 2); // remove 2 items from index 1
console.log(removed); // [2, 3]
console.log(nums); // [1, 4, 5]
nums.splice(1, 0, "x", "y"); // insert at index 1, remove nothing
console.log(nums); // [1, "x", "y", 4, 5]
nums.splice(3, 1, "z"); // replace one item
console.log(nums); // [1, "x", "y", "z", 5]
nums.splice(2); // remove everything from index 2
console.log(nums); // [1, "x"]The return value trips people up. It is never the updated array. It is always the list of removed items, so an insert-only call returns []. Code such as const list = items.splice(i, 1) almost always means the author wanted the remaining items and got the deleted one instead.
There is one quirk with the second argument. Leaving it out removes everything to the end, but passing undefined explicitly counts as 0, so arr.splice(2, undefined) removes nothing. That bites when the count comes from a variable that was never set.
Negative indexes
Both methods read a negative index as "count back from the end": -1 is the last item, -2 the one before it.
const letters = ["a", "b", "c", "d", "e"];
letters.slice(-2); // ["d", "e"]
letters.slice(-3, -1); // ["c", "d"]
const nums = [1, 2, 3, 4, 5];
nums.splice(-2, 1); // returns [4]
console.log(nums); // [1, 2, 3, 5]An index past the end is clamped to the length, and a negative index larger than the length is treated as 0. Neither method throws for out-of-range values.
toSpliced(): splice without mutation
toSpliced() takes the same arguments as splice() but returns a new array with the change applied and leaves the original alone. It is one of the change-array-by-copy methods, with toSorted(), toReversed() and with(), and it is supported in all current browsers and Node.
const todos = ["write", "test", "ship"];
const withoutTest = todos.toSpliced(1, 1); // ["write", "ship"]
const withReview = todos.toSpliced(2, 0, "review"); // ["write", "test", "review", "ship"]
console.log(todos); // ["write", "test", "ship"] (unchanged)Note the different return value. toSpliced() gives you the new array, not the removed items. If you need both, read the removed items with slice() first.
Before toSpliced(), the usual non-mutating removal was [...arr.slice(0, i), ...arr.slice(i + 1)] or arr.filter((_, index) => index !== i). Both still work.
Why the difference matters in real code
Mutation is the main reason interviewers ask this. In React, Redux or any code that compares references to detect changes, splice on state edits the existing array, so the reference stays the same and the update can be missed. Use slice, toSpliced, filter or spread instead, as described in what is a pure function, and why does immutability matter. splice is still the right tool when you own the array and want to change it, for example inside a function that builds a local result.
Practise it
Predict array mutation results checks the return values and side effects above. Then write an Array.prototype.splice polyfill and polyfill toSorted, toReversed, toSpliced and with, and compare string methods in Predict string method results.
Common follow-up questions
How do you remove an item at an index?
To change the array: arr.splice(i, 1). To get a new array: arr.toSpliced(i, 1) or arr.filter((_, index) => index !== i). To remove by value, find the index first with indexOf or findIndex and check it is not -1, because splice(-1, 1) would remove the last item.
How do you remove several items while looping?
Loop backwards, or build a new array with filter. Splicing while looping forwards shifts the later items down one place, so the loop skips the item that moved into the current index.
What is the difference between slice and substring on strings?
slice and substring on strings?Both return part of a string without changing it. slice supports negative indexes. substring treats negatives as 0 and swaps its arguments if start is greater than end, so "hello".substring(3, 1) is "el" while "hello".slice(3, 1) is "".
Does slice work on arguments or a NodeList?
slice work on arguments or a NodeList?The old idiom Array.prototype.slice.call(arguments) turns an array-like into a real array. Today Array.from() or spread does the same job more clearly. See array-like objects.
Mistakes to avoid
Expecting
spliceto return the updated arrayIt returns the removed items.
Reading the second argument of
spliceas an end indexIt is a count.
Calling
spliceon React or Redux state and wondering why nothing re-rendersPassing
undefinedas the delete count and expecting it to remove to the endAssuming
slice()makes a deep copyNested objects are still shared.
Using
splice(indexOf(x), 1)without checking for-1
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Predict array mutation results:
splice,fillwith shared objects,toSorted,withandcopyWithinEasy25 min - Write an
Array.polyfill: negative indexes, omittedprototype. splice deleteCountand insertsEasy15 min - Polyfill
toSorted,toReversed,toSplicedandwith: change-array-by-copy with holes, negative indexes and no mutationEasy25 min - Predict string method results:
sliceversussubstring,splitwith a limit,padStart,replaceandatEasy20 min