1st Argument: Specifies from where the selection should be started
2nd Argument: Specifies at which level the endpoint should be. If you didn’t put this in the parenthesis
while calling the slice method, it will return the elements from the starting index to the end of the array.
Note:
Slice always returns the selected elements from the array.
Slice won’t change the array. The array remains intact
var arr1 = [1,5,8,9];
arr1.slice(1); // [5,8,9]
arr1.slice(1,3); //[ 5, 8 ]
arr1.slice(-2);//[ 8, 9 ]
1st Argument: Specifies at which position a new element or existing element should be added/removed. If the
value is negative the position will be counted from the end of the array.
2nd Argument: The numbers of elements to be removed from the starting position. If it is 0, then no elements
will be removed. If it is not passed, it will delete all elements from the starting position.
3rd Argument ->nth Argument: The value of the items you want to add to the array.
var arr1 = [1,5,8,9];
arr1.splice(1,2);// [ 5, 8 ]
arr1.splice(1,2,'Hi','Medium')
Note:
Splice will return removed elements from the array only.
Splice will change the original array
var arr1 = [1,3,6,7];
var arr2 = [5,arr1,8,9]; // [ 5, [ 1, 3, 6, 7 ], 8, 9 ]
Definition: Allows an iterable such as an array expression or string to be expanded in places where zero or
more arguments (for function calls) or elements (for array literals) are expected, or an object expression to
be expanded in places where zero or more key-value pairs (for object literals) are expected.
var arr2 = [5,...arr1,8,9]; //[ 5, 1, 3, 6, 7, 8, 9 ]
//Copy an array
var arr = [1, 2, 3];
var arr2 = [...arr]; // like arr.slice()