JavaScript Array splice()
Syntax & Examples
Array.splice() method
The splice() method of the Array class in JavaScript adds and/or removes elements from an array.
Syntax of Array.splice()
There are 5 variations for the syntax of Array.splice() method. They are:
splice(start)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. |
This method removes all elements from the start index to the end of the array.
Returns value of type Array
.
splice(start, deleteCount)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. |
deleteCount | optional | The number of elements to remove from the start index. |
This method removes the specified number of elements from the start index.
Returns value of type Array
.
splice(start, deleteCount, item1)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. |
deleteCount | optional | The number of elements to remove from the start index. |
item1 | optional | The element to add to the array, beginning at the start index. |
This method removes the specified number of elements from the start index and adds the specified item.
Returns value of type Array
.
splice(start, deleteCount, item1, item2)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. |
deleteCount | optional | The number of elements to remove from the start index. |
item1 | optional | The first element to add to the array, beginning at the start index. |
item2 | optional | The second element to add to the array, beginning at the start index. |
This method removes the specified number of elements from the start index and adds the specified items.
Returns value of type Array
.
splice(start, deleteCount, item1, item2, /* …, */ itemN)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. |
deleteCount | optional | The number of elements to remove from the start index. |
item1 | optional | The first element to add to the array, beginning at the start index. |
item2 | optional | The second element to add to the array, beginning at the start index. |
itemN | optional | Additional elements to add to the array, beginning at the start index. |
This method removes the specified number of elements from the start index and adds the specified items.
Returns value of type Array
.
✐ Examples
1 Using splice() method to remove elements from an array
In JavaScript, we can use the splice() method to remove elements from an array.
For example,
- We define an array variable arr with elements [1, 2, 3, 4, 5].
- We use the splice() method on arr with the start index 1 and deleteCount 2 to remove two elements starting from index 1.
- The result is stored in the variable removedElements.
- We log removedElements and the updated array to the console using console.log() method.
JavaScript Program
const arr = [1, 2, 3, 4, 5];
const removedElements = arr.splice(1, 2);
console.log(removedElements); // Output: [2, 3]
console.log(arr); // Output: [1, 4, 5]
Output
[2, 3] [1, 4, 5]
2 Using splice() method to add elements to an array
We can use the splice() method to add elements to an array.
For example,
- We define an array variable arr with elements ['a', 'b', 'e'].
- We use the splice() method on arr with the start index 2, deleteCount 0, and items 'c' and 'd' to add the elements 'c' and 'd' at index 2.
- The result is stored in the variable removedElements.
- We log removedElements and the updated array to the console using console.log() method.
JavaScript Program
const arr = ['a', 'b', 'e'];
const removedElements = arr.splice(2, 0, 'c', 'd');
console.log(removedElements); // Output: []
console.log(arr); // Output: ['a', 'b', 'c', 'd', 'e']
Output
[] ['a', 'b', 'c', 'd', 'e']
3 Using splice() method to replace elements in an array
We can use the splice() method to replace elements in an array.
For example,
- We define an array variable arr with elements [1, 2, 3, 4, 5].
- We use the splice() method on arr with the start index 1, deleteCount 2, and items 'a' and 'b' to replace two elements starting from index 1 with 'a' and 'b'.
- The result is stored in the variable removedElements.
- We log removedElements and the updated array to the console using console.log() method.
JavaScript Program
const arr = [1, 2, 3, 4, 5];
const removedElements = arr.splice(1, 2, 'a', 'b');
console.log(removedElements); // Output: [2, 3]
console.log(arr); // Output: [1, 'a', 'b', 4, 5]
Output
[2, 3] [1, 'a', 'b', 4, 5]
Summary
In this JavaScript tutorial, we learned about splice() method of Array: the syntax and few working examples with output and detailed explanation for each example.