The splice() method is utilized for adding new items to an array.
Example
| const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.splice(2, 0, “Lemon”, “Kiwi”); |
The first parameter (2) specifies the position where new elements should be inserted (spliced in).
The second parameter (0) specifies the number of elements to be removed.
The remaining parameters (“Lemon”, “Kiwi”) denote the new elements to be added.
Upon execution, the splice() method returns an array containing the deleted items.
Example
| const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.splice(2, 2, “Lemon”, “Kiwi”); |