Both the slice () method and the splice () method are the methods of the array operation in native JS.
Slice () returns a new array that returns the selected element from an existing array. For example: Arrobject (Start,end), start is required. Specifies where to start the selection, and if it is a negative number, it is selected from the end of the array element, that is, 1 refers to the last element, 2 refers to the second-lowest element, and end is an optional element. Specifies where to end the selection. This parameter does not indicate that the end of the array is intercepted from the beginning and, if it is negative, that the element is intercepted forward from the end of the array. The method does not modify the original array, and if you want to delete an element of an array, use the splice () method.
Splice (), add/Remove elements from the array. For example: Arrayobject.splice (index,howmany,item1,....., ItemX). Index is required to specify the location of the Add/remove item. The Howmany must be an entry that represents the number of deleted items, and if 0 indicates that the element is not deleted. Splice () removes Howmany elements starting with index and can replace deleted elements with the item element. The item optional parameter, which represents the newly added item.
Usage such as:
(1):
var arr = new Array (5);
Arr[0] = "Amy";
ARR[1] = "Elice";
ARR[2] = "divi";
ARR[3] = "Lvy";
Arr[4] = "Marry";
Arr.splice (1, 0, "Willian");
Console.log (arr);
Output: Amy,willian,elice,divi,lvy, adding an element at the first position of the array with a value of "Willian"
(2):
var arr = new Array (5);
Arr[0] = "Amy";
ARR[1] = "Elice";
ARR[2] = "divi";
ARR[3] = "Lvy";
Arr[4] = "Marry";
Arr.splice (1, 2, "Willian");
Console.log (arr);
Output: Amy,willian,lvy, removes two elements from the first position of the array, and replaces the deleted element with the new element "Willian".
Slice () method and Splice () difference in native JS