What is the purpose of array slice method?
The slice() method returns the selected elements in an array as a new array object. It selects the elements starting at the given start argument, and ends at the given optional end argument without including the last element. If you omit the second argument then it selects till the end. Some of the examples of this method are,
let arrayIntegers = [1, 2, 3, 4, 5];let arrayIntegers1 = arrayIntegers.slice(0,2); // returns [1,2]let arrayIntegers2 = arrayIntegers.slice(2,3); // returns [3]let arrayIntegers3 = arrayIntegers.slice(4); //returns [5]
Note: Slice method won't mutate the original array but it returns the subset as new array.
What is the purpose of array splice method?
The splice() method is used either adds/removes items to/from an array, and then returns the removed item. The first argument specifies the array position for insertion or deletion whereas the option second argument indicates the number of elements to be deleted. Each additional argument is added to the array. Some of the examples of this method are,
let arrayIntegersOriginal1 = [1, 2, 3, 4, 5];let arrayIntegersOriginal2 = [1, 2, 3, 4, 5];let arrayIntegersOriginal3 = [1, 2, 3, 4, 5];let arrayIntegers1 = arrayIntegersOriginal1.splice(0,2); // returns [1, 2]; original array: [3, 4, 5]let arrayIntegers2 = arrayIntegersOriginal2.splice(3); // returns [4, 5]; original array: [1, 2, 3]let arrayIntegers3 = arrayIntegersOriginal3.splice(3, 1, "a", "b", "c"); //returns [4]; original array: [1, 2, 3, "a", "b", "c", 5]
Note: Splice method modifies the original array and returns the deleted array.
What is the difference between slice and splice?
Some of the major difference in a tabular form
Slice | Splice |
---|---|
Doesn't modify the original array(immutable) | Modifies the original array(mutable) |
Returns the subset of original array | Returns the deleted elements as array |
Used to pick the elements from array | Used to insert or delete elements to/from array |