JavaScriptQuestion 18 of 36
Difference between slice and splice?
Answer out loud or in your head first, then compare.
Answer
slice doesn't mutate the array.
JavaScript
const arr = [1, 2, 3, 4];
arr.slice(1, 3);
// [2, 3]arr is still:
JavaScript
[1, 2, 3, 4]splice mutates:
JavaScript
arr.splice(1, 2);now:
JavaScript
[1, 4]Remember:
slice → copies a part
splice → changes the originalHow did it go? Be honest — “Not yet” puts the question on your review list.
Related questions
- slice or splice — which changes the array? — JavaScript