1var colors=["red","blue"];
2var index=1;
3
4//insert "white" at index 1
5colors.splice(index, 0, "white"); //colors = ["red", "white", "blue"]
6
1// Syntax
2splice(start)
3splice(start, deleteCount)
4splice(start, deleteCount, item1)
5splice(start, deleteCount, item1, item2, itemN)
6
7const months = ['Jan', 'March', 'April', 'June'];
8months.splice(1, 0, 'Feb');
9// inserts at index 1
10console.log(months);
11// expected output: Array ["Jan", "Feb", "March", "April", "June"]
12
13months.splice(4, 1, 'May');
14// replaces 1 element at index 4
15console.log(months);
16// expected output: Array ["Jan", "Feb", "March", "April", "May"]
1let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
2//insert new element into array at index 2
3let removed = myFish.splice(2, 0, 'drum')
4
5// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
6// removed is [], no elements removed
1Check Out my CODE-PEN for this Solution
2
3https://codepen.io/siddhyaOP/pen/eYgRGOe
4https://codepen.io/siddhyaOP/pen/eYgRGOe
5https://codepen.io/siddhyaOP/pen/eYgRGOe
1const numbers = [2, 4, 5, 3, 8, 9, 11, 33, 44];
2const spliceNumbers = numbers.splice(0, 5)
3//start from 0 && remove next 5 elements
4console.log(spliceNumbers)
5//Expected output:[ 2, 4, 5, 3, 8 ]
1Splice and Slice both are Javascript Array functions. Splice vs Slice. The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object. The splice() method changes the original array and slice() method doesn't change the original array.27-Jan-2018