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);
Example 3: array.splice javascript
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
console.log(months);
months.splice(4, 1, 'May');
console.log(months);
months.splice(0, 1);
console.log(months);
Example 4: what is splice in javascript
let colors = ['red', 'blue', 'green'];
let index_element_to_be_delete = colors.indexOf('green');
colors.splice(index_element_to_be_delete);