What is the difference between slice and splice?

const a = [1, 2, 3, 4, 5];
console.log(a.slice(1, 3));
console.log(a);

const removed = a.splice(1, 2);
console.log(removed);
console.log(a);

Output:

Tutorials Class - Output Window

[ 2, 3 ]
[ 1, 2, 3, 4, 5 ]
[ 2, 3 ]
[ 1, 4, 5 ]

slice(start, end) is non-destructive. splice(start, count, ...items) changes the array. See JavaScript Arrays.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.