What is the difference between slice and splice?
Answer
slice(start, end) returns a shallow copy of a portion of the array from start up to (but not including) end. It does not modify the original array. Negative indices count from the end. arr.slice(1, 3) returns elements at index 1 and 2. arr.slice(-2) returns the last 2 elements. arr.slice() (no args) creates a shallow copy of the full array. splice(start, deleteCount, ...items) modifies the original array by removing, replacing, or inserting elements in place. It returns an array of removed elements. arr.splice(1, 2) removes 2 elements starting at index 1. arr.splice(1, 0, "new") inserts "new" at index 1 without removing anything. arr.splice(1, 1, "replacement") replaces 1 element. Memory trick: slice is nice (non-destructive); splice delices (destructive).