splice allows us to add, replace or remove elements in an array without creating a new array.

Structure

splice (startIndex, numberOfDeletions, elementToInsert)

splice (startIndex, numberOfDeletions, elementToInsert, anotherElementToInsert)

Examples

Insert element into array at a certain position
const array1 = [1, 2, 3, 5];
array1.splice(3, 0, 4); //Insert at position 3, delete 0 elements, insert the number 4.
console.log(array1); //[1, 2, 3, 4, 5];
Replace element in array at a certain position
const array1 = [1, 2, 3, 5];
array1.splice(3, 1, 4); //Insert at position 3, delete 1 element, insert the number 4.
console.log(array1); //[1, 2, 3, 4];
Add elements at the end of the array
const array1 = [1, 2, 3, 7];
array1.splice(3, 1, 4, 5); //Insert at position 3, delete 1 element, insert the numbers 4 and 5.
console.log(array1); //[1, 2, 3, 4, 5]