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.
Array indexes start from 0, so if you want to add the item first, you'll use index 0, in the second place the index is 1, and so on.
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
Waiting for your comments