differrence between slice and splice code example
Example 1: javascript slice vs splice
var array=[1,2,3,4,5]
console.log(array.slice(2));
console.log(array.slice(-2));
console.log(array);
var array2=[6,7,8,9,0];
console.log(array2.slice(2,4));
console.log(array2.slice(-2,4));
console.log(array2.slice(-3,-1));
console.log(array2);
Example 2: javascript slice vs splice
var array=[1,2,3,4,5];
console.log(array.splice(2));
console.log(array);
var array2=[6,7,8,9,0];
console.log(array2.splice(2,1));
console.log(array2.splice(2,0));
console.log(array2);
var array3=[11,12,13,14,15];
console.log(array3.splice(2,1,"Hello","World"));
console.log(array3);
-5 -4 -3 -2 -1
| | | | |
var array4=[16,17,18,19,20];
| | | | |
0 1 2 3 4
console.log(array4.splice(-2,1,"me"));
console.log(array4);