If the user wants to add an item to an array, but you don't want to append an item at the end or start of the array. You want to directly allow for the insertion of an element at a particular place (index) of the array.
We can use it for the for-loop method to do this. But we are using the splice() method of an array to perform this type of operation.
- The first is the start index, the index where the element id is to be inserted.
- The second is the delete count parameter, the number of items to be deleted, and the new items which are to be inserted.
- This allows to only insert the specified item at a particular index with no deletion.
Output: Before Click on Button
Output: After Click on Button