Javascript splice() Method - Array Function
How to insert and delete elements from the array?
What is the use of splice() function in javascript?
Explanation
Method: splice() :
This function is used to insert or delete element in the given array. It returns deleted items.
Note: The method modifies the value of the original array.
Syntax:array1.splice(argument1,argument2,.....);
argument1-specifies the array position in which the insertion/deletion to be performed.
argument2-specifies the number of elements should be deleted.
The arguments following after second argument specifies the elements that are to be included after the first argument.
Example:
Deleting elements
<script type="text/javascript">
var name = ["john", "peter","samuels","joseph","marry"];
var subarray = name.splice(1,3);
document.write(subarray);
</script>
Result:
Example Code:Inserting elements
<script type="text/javascript">
var name = ["john", "peter","samuels","joseph","marry"];
name.splice(1,0,"reobuck","ram");
document.write(name);
</script>
Result: