Array.splice() method is used to copy a given part of the array. It returns the copied array as a new array without changing the original array.
Here is the syntax of Array.slice() method
As we can see, Array.slice() method takes two arguments. The first arguments define the starting index i.e from where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array and the second argument defines the ending index.
Here is a working example of Array.slice()
const array=[1,2,3,4,5] console.log(array.slice(2)); // Output [3, 4, 5], returned selected element(s). console.log(array.slice(-2)); // Output [4, 5], returned selected element(s). console.log(array); // shows [1, 2, 3, 4, 5], original array remains intact. const array2=[6,7,8,9,0]; console.log(array2.slice(2,4)); // output [8, 9]
The Array.splice( ) method changes the original array, by adding or removing elements from it and return the removed items in a new array.
Here is the syntax of Array.splice() method
array.splice(index, number of elements);
Array.splice() method also takes two arguments. The first argument i.e. Index is the starting point for removing elements. Elements that have a smaller index number from the given index won’t be removed.
Here is a working example of the same
array.splice(2); // Every element starting from index 2, will be removed
The second arguments define the number of elements to be removed from the starting index. If you choose the starting index as two and the second argument value is four, then the elements starting from the index two and ending on the index four will be removed. Here is an example of Array.splice()
const array=[1,2,3,4,5]; console.log(array.splice(2)); // shows [3, 4, 5], returned removed item(s) as a new array object. console.log(array); // shows [1, 2], original array altered. const array2=[6,7,8,9,0]; console.log(array2.splice(2,1)); // shows  console.log(array2.splice(2,0)); //shows  , as no item(s) removed.
Difference between Array.slice() and Array.splice()
|Returns the selected item in an array||Returns the removed item in an array|
|Doesn’t Change the original array||Change the original array|