Instance
Move the third element of the divisor group and add the new element in the third position of the array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
Fruits.splice (2,1, "Lemon", "Kiwi");
Fruits Output results:
Banana,orange,lemon,kiwi,mango Definition and usage
The splice () method is used to insert, delete, or replace elements of an array.
Note: This method will change the original array!.
Browser support
All major browsers support splice ().
Grammar
Array. Splice (
Index,
Howmany,
item1,.....,
ItemX) parameter values
| Parameters |
Description |
| Index |
Necessary. Specifies where to add/remove elements from. This parameter is the subscript that begins inserting and/or deleting an array element, which must be a number. |
| Howmany |
Necessary. Specifies how many elements should be deleted. Must be a number, but it can be "0". If this parameter is not specified, all elements from index start to the end of the original array are deleted. |
| item1, ..., ItemX |
Optional. The new element to add to the array |
return value
| Type |
Description |
| Array |
If the element is removed from the Arrayobject, the array containing the deleted element is returned. |
Usage of splice () in JS