Articles → JAVASCRIPT → Splice Functions In Javascript
Splice Functions In Javascript
Purpose
Syntax
- The first parameter indicates the index of the array where the item can be added or removed.
- The second parameter is the count of the number of elements to be deleted. If the count is zero then it means that we are adding the value in an array.
- The third parameter is an array of items to be inserted.
Add An Element In An Array
<script>
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb'); // => Inserts the value at index 1
months.splice(2, 0, 'Feb'); // => Inserts the value at index 2
months.splice(3, 0, 'Feb'); // => Inserts the value at index 3
console.log(months);
</script>
Try It
Removing Elements In An Array
<script>
const months = ['Jan', 'March', 'April', 'June'];
months.splice(3, 1, 'May'); // => Delete
console.log(months);
</script>
Try It
Posted By - | Karan Gupta |
|
Posted On - | Sunday, September 5, 2021 |