JavaScript Notes
Master JavaScript splice() and slice(): add/remove/replace elements, extract portions, mutation behavior, comparison table, real-world use cases, and interview Q&A.
splice() and slice() sound similar but behave very differently. One mutates; one doesn't. One is for editing; one is for copying. Get these straight and you'll never confuse them again.
Quick Comparison
splice() → MUTATES original array → add, remove, replace elements
slice() → NEW array → copy a portion of the array
Memory trick:
"sp-LICE" has an extra letter "p" → "Plus" work = mutates
"s-LICE" is lean → just reads/copiesDelete Elements
Removed: [2, 3] Array: [1, 4, 5]
Insert Elements (deleteCount = 0)
[1, "a", "b", 4, 5]
Replace Elements
[1, "X", "Y", "Z", 4, 5]
Negative Index
[1, 2, 3] [1, 2]
All splice() Operations
After delete: ["red", "green", "yellow", "purple"] After insert: ["red", "cyan", "magenta", "green", "yellow", "purple"] After replace: ["orange", "cyan", "magenta", "green", "yellow", "purple"]
slice() — The Non-Mutating Extractor
slice() copies a portion of an array into a new array, leaving the original untouched.
Syntax
array.slice(start, end) // end is EXCLUSIVE
// Returns: new array with elements from start up to (not including) end
// Mutates: NO| Parameter | Description |
|---|---|
start | Index to begin extraction (inclusive) |
end | Index to stop extraction (exclusive — not included) |
Basic slice() Examples
[2, 3] [3, 4, 5] [4, 5] [3, 4] [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
Visual: slice() Index Reference
Shallow Copy with slice()
Original: [1, 2, 3] Copy: [1, 2, 3, 99] Nested original: 999
splice() vs slice() — Full Comparison
| Feature | splice() | slice() |
|---|---|---|
| Mutates original | ✅ YES | ❌ NO |
| Returns | Array of removed elements | New array with extracted elements |
| Can insert? | ✅ Yes | ❌ No |
| Can delete? | ✅ Yes | ❌ No (just reads) |
| Can replace? | ✅ Yes | ❌ No |
end parameter | deleteCount (how many) | end index (exclusive) |
| Negative index | ✅ Yes | ✅ Yes |
| Immutable alternative | toSpliced() (ES2023) | Already non-mutating |
| Use case | Edit the array | Copy a portion |
slice result: ["b", "c", "d"] original after slice: ["a", "b", "c", "d", "e"] splice removed: ["b", "c"] original after splice: ["a", "d", "e"]
toSpliced() — Non-Mutating splice (ES2023)
toSpliced result: [1, "X", "Y", 4, 5] Original unchanged: [1, 2, 3, 4, 5]
Real World Use Cases
["apple", "cherry"] [1, 2, 3, 4, 5] Page 2: [4, 5, 6] Last 3: ["d", "e", "f"]
Common Mistakes
| ❌ Wrong | ✅ Correct | Why |
|---|---|---|
arr.slice(1, 3) expecting index 3 included | slice(1, 3) returns indices 1 and 2 only | end in slice() is exclusive |
arr.splice(1, 3) expecting to extract | Use arr.slice(1, 3) to extract without mutation | splice mutates; slice extracts |
Confusing splice(start, deleteCount) with slice(start, end) | splice's 2nd arg = HOW MANY, slice's 2nd arg = WHERE TO STOP | Different meanings for 2nd argument! |
arr.splice(1, 0) expecting deletion | arr.splice(1, 1) to delete 1 element | deleteCount = 0 means insert, not delete |
Forgetting splice() returns removed elements | const removed = arr.splice(...) | Returned array is the deleted elements |
Cheat Sheet
splice — MUTATING:
Delete: arr.splice(index, count)
Insert: arr.splice(index, 0, item1, item2)
Replace: arr.splice(index, count, newItem)
Returns: array of removed elements
slice — NON-MUTATING:
Get portion: arr.slice(start, end) ← end is exclusive
From index: arr.slice(2) ← from index 2 to end
Last N: arr.slice(-N) ← last N elements
Full copy: arr.slice()
Returns: new array
Immutable alternatives (ES2023):
toSpliced(start, deleteCount, ...items) ← new array, no mutationInterview Questions
Q1. What is the key difference between splice() and slice()?
splice() mutates the original array and can add, remove, or replace elements. slice() does not mutate the original — it returns a new array with a portion of the original.
Q2. What does splice() return?
An array of the removed elements. If deleteCount is 0 (insert only), it returns an empty array [].
Q3. What does slice() return?
A new array containing the extracted elements. The original array is unchanged.
Q4. What is the difference between splice(start, deleteCount) and slice(start, end)?
In splice(), the second argument is how many elements to remove (deleteCount). In slice(), the second argument is the index where extraction stops (exclusive end index). This is the most common confusion!
Q5. How do you remove a specific element from an array using splice()?
First find the index: const idx = arr.indexOf(item), then if idx !== -1, call arr.splice(idx, 1).
Q6. How do you create a shallow copy of an array using slice()?
const copy = arr.slice() — calling slice() with no arguments returns a full shallow copy.
Q7. What is toSpliced() (ES2023)?
toSpliced() is the non-mutating version of splice(). It returns a new array with the specified changes applied, leaving the original untouched.
Q8. How would you insert an element at a specific index without mutating the original?
Key Takeaways
splice()mutates — use it to add, remove, or replace in placeslice()does not mutate — use it to copy a portionsplice(start, deleteCount)— second arg = HOW MANY to removeslice(start, end)— second arg = WHERE to stop (exclusive)splice()returns the removed elements;slice()returns the extracted copy- Use
[...arr].splice()ortoSpliced()(ES2023) for non-mutating splice slice(-N)is a clean way to get the last N elements
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript splice() vs slice() - Complete Tutorial with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, arrays, splice, slice
Related JavaScript Master Course Topics