Table of Contents
The Short Answer
slice() copies: it returns a piece of the array and does not change the original.
splice() edits: it changes the original array and returns the items it removed.
let nums = [10, 20, 30, 40, 50]; let sliced = nums.slice(1, 3); console.log(sliced); // [20, 30] console.log(nums); // [10, 20, 30, 40, 50], unchanged let spliced = nums.splice(1, 2); console.log(spliced); // [20, 30] console.log(nums); // [10, 40, 50], changed
What slice() Does
Copies a portion of an array: you choose where to start and where to stop.
Returns a new array: the result is separate from the original.
Leaves the original alone: nothing is removed or changed.
Excludes the end index: it copies up to, but not including, that position.
let fruits = ["apple", "banana", "mango", "orange"]; console.log(fruits.slice(1, 3)); // ["banana", "mango"] console.log(fruits.slice(2)); // ["mango", "orange"] console.log(fruits.slice(-2)); // ["mango", "orange"] console.log(fruits.slice()); // a full copy
What splice() Does
Removes items: you choose where to start and how many to remove.
Adds items: you can insert new ones at any position.
Replaces items: remove some and add others in the same call.
Changes the original array: the edit happens in place.
Returns the removed items: as a new array.
let colors = ["red", "green", "blue", "yellow"]; colors.splice(1, 1); // remove one item at index 1 console.log(colors); // ["red", "blue", "yellow"] colors.splice(1, 0, "pink"); // add an item at index 1 console.log(colors); // ["red", "pink", "blue", "yellow"] colors.splice(2, 1, "orange"); // replace the item at index 2 console.log(colors); // ["red", "pink", "orange", "yellow"]
Comparing Them Side by Side
| Feature | slice() | splice() |
| Changes the original array | No | Yes |
| What it returns | A new array of the copied items | An array of the removed items |
| Can remove items | No | Yes |
| Can add items | No | Yes |
| Can replace items | No | Yes |
| Second argument | End index, not included | Number of items to remove |
| Supports negative indexes | Yes | Yes, for the start index |
| Works on strings | Yes | No |
The Second Argument Is Different
slice() takes an end position: it is an index, and the item at that index is not included.
splice() takes a count: it is the number of items to remove.
let nums = [1, 2, 3, 4, 5]; console.log(nums.slice(1, 3)); // [2, 3], from index 1 up to index 3 console.log([...nums].splice(1, 3)); // [2, 3, 4], three items starting at index 1This is the most common source of confusion: the same two numbers give different results.
Practical Examples
Taking the first three items safely:let results = ["a", "b", "c", "d", "e"]; let topThree = results.slice(0, 3); console.log(topThree); // ["a", "b", "c"] console.log(results); // all five items remainRemoving an item from a list:
let tasks = ["study", "gym", "sleep"]; tasks.splice(1, 1); console.log(tasks); // ["study", "sleep"]Making a copy before editing:
let original = [1, 2, 3, 4]; let copy = original.slice(); copy.splice(1, 1); console.log(original); // [1, 2, 3, 4] console.log(copy); // [1, 3, 4]Removing an item without changing the original:
let list = ["a", "b", "c", "d"]; let withoutSecond = [...list.slice(0, 1), ...list.slice(2)]; console.log(withoutSecond); // ["a", "c", "d"] console.log(list); // ["a", "b", "c", "d"]Pagination:
let items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let page = 2; let size = 3; console.log(items.slice((page - 1) * size, page * size)); // [4, 5, 6]
Newer Alternatives
toSpliced(): works like splice(), but returns a new array and leaves the original alone.
toSorted() and toReversed(): follow the same idea for sorting and reversing.
let nums = [1, 2, 3, 4]; let updated = nums.toSpliced(1, 1); console.log(updated); // [1, 3, 4] console.log(nums); // [1, 2, 3, 4], unchangedThese are supported in modern environments: check compatibility before using them in older projects.
When to Use Each One
Use slice() when:
- You need a portion of an array without changing it.
- You want to make a copy of an array.
- You are building pagination or showing the first or last few items.
- You are working with state that should not be changed directly, such as in React.
Use splice() when:
- You need to remove an item at a known position.
- You need to insert an item in the middle of an array.
- You need to replace an item in place.
- Changing the original array is exactly what you want.
Common Mistakes
Mixing up the two names: one is safe, and the other changes your array.
Treating the second argument the same way: slice() takes an end index, while splice() takes a count.
Expecting splice() to return the updated array: it returns the removed items.
Forgetting that splice() changes the original: this causes bugs when the same array is used elsewhere.
Splicing with an index of -1 by accident: this often happens after indexOf() finds nothing, and it removes the last item.
let letters = ["a", "b", "c"];
let index = letters.indexOf("z"); // -1
letters.splice(index, 1);
console.log(letters); // ["a", "b"], the last item was removed by mistake
Conclusion
The easiest way to remember the difference is that slice() gives you a piece, while splice() cuts into the array itself. Use slice() when you want to keep the original safe, and splice() when you want to change it on purpose. Always check the second argument too, because one is an end index and the other is a count.Frequently Asked Questions (FAQs)
1.Does slice() change the original array?2.Does splice() change the original array?No. It always returns a new array.
3.Which one should I use to remove an item?Yes. It edits the array in place and returns the removed items.
Use splice() to remove it from the original array, or filter() or slice() with the spread operator if you want to keep the original unchanged.
0 Comments