JavaScript · Junior · Question 26 of 75
How does slice differ from splice?
- ✓Correct answer:
slicereturns part of the array without changes,splicemutates the original - 2
splicereturns an unchanged copy, whileslicemutates the original array - 3They're synonyms;
spliceis the deprecated name ofslice
Explanation
arr.slice(1, 3) creates a new array. arr.splice(1, 2, 'x') removes and inserts elements in arr itself. The non-mutating alternative is toSpliced(). Other mutating methods: push, pop, shift, sort, reverse.