In this tutorial, we are going to learn about the javascript array splice()
method with the help of multiple examples.
The array splice()
method in javascript is used to remove, add and replace elements in an array. You can change and add elements to your original array with the help of the splice()
method. When you will remove the elements of an array using the splice()
method it will return a new array of removed elements.
Example
let months = ["January", "February", "Monday", "Tuesday"];
//removing elements from index 2
//adding new elements from index 2
let removed = months.splice(2, 2, "March", "April");
console.log(removed);
// Output: [ 'Monday', 'Tuesday' ]
console.log(months);
// Output: [ 'January', 'February', 'March', 'April' ]
Also Read:
Array splice() Syntax
arr.splice(index, deleteCount, item1, ....., itemN)
Here arr
is an array.
Array splice() Parameters
index: array index from where you want to remove or add the elements (Required).
deleteCount: number of elements you want to remove from index
(Optional).
item1, ….., itemN : these are the items that you want to add to the array from index
. If not specified splice()
will only remove the elements (Optional).
Array splice() Return Value
Array splice()
returns the removed elements in a new array.
Note: As I mentioned above, the splice()
method changes the original array.
Example 1: Using Array splice() method.
let months = ["January", "February", "Monday", "Tuesday"];
//using only index parameter
let removed = months.splice(2);
console.log(removed);
// Output: [ 'Monday', 'Tuesday' ]
console.log(months);
// Output: [ 'January', 'February' ]
//removing elements from index 2
//adding new elements from index 2
let removed2 = months.splice(2, 2, "March", "April", "May");
console.log(removed2);
// Output: []
console.log(months);
// Output: [ 'January', 'February', 'March', 'April' ]
//using index and deleteCount Parameters
let removed3 = months.splice(2, 3);
console.log(removed3);
// Output: [ 'March', 'April', 'May' ]
console.log(months);
// Output: [ 'January', 'February' ]
Output
[ 'Monday', 'Tuesday' ]
[ 'January', 'February' ]
[]
[ 'January', 'February', 'March', 'April', 'May' ]
[ 'March', 'April', 'May' ]
[ 'January', 'February' ]
Example 2: Using Array splice() with Negative values.
If we are using negative values ( index<0 ) then the index is counted from the last element of the array. For example, -1
is the index of the last element of the array.
let months = ["January", "February", "Monday", "Tuesday"];
//using only negative index value
let removed = months.splice(-2);
console.log(removed);
// Output: [ 'Monday', 'Tuesday' ]
console.log(months);
// Output: [ 'January', 'February' ]
//removing elements from index 2
//adding new elements from index -2
let removed2 = months.splice(2, -2, "March", "April");
console.log(removed2);
// Output: []
console.log(months);
// Output: [ 'January', 'February', 'March', 'April' ]
Output
[ 'Monday', 'Tuesday' ]
[ 'January', 'February' ]
[]
[ 'January', 'February', 'March', 'April' ]