Spot splice vs slice Mistake
Question
This code is supposed to create a copy of part of the array without modifying the original, but it modifies the original. What is wrong?
Code
const original = [1, 2, 3, 4, 5];
const copy = original.splice(1, 3);
console.log(original);
console.log(copy);
Hint
splice mutates; slice does not.
Explanation
splice removes elements from the original array and returns them — it mutates original, which becomes [1, 5]. slice(1, 4) would return [2, 3, 4] without modifying the original. Use slice when you want a non-mutating copy of a portion of the array.
Related Resources
Related Lessons
- JavaScript Arrays
Learn about splice vs slice.
Related Reference
- JavaScript Array Reference
Reference for splice and slice.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.