Using Splice method in Javascript

Posted by vishalneeraj-24503 under JavaScript category on | Points: 40 | Views : 1645
The Javascript Splice() method is used to add or remove any items to or from an array,and returns the removed item(s). This method changes the original array.
Javascript array splice() method changes the content of an array,adding new elements while removing old elements.

we can understand it by an example:-

<script type="text/javascript">

function splice_example()
var arr = ["orange", "mango", "banana", "sugar", "tea"];

var removed_item = arr.splice(2, 0,"pineapple");
document.write("After adding 1: " + arr );
document.write("<br />removed_item is: " + removed_item);

removed_item = arr.splice(3, 1);
document.write("<br />After adding 1: " + arr);
document.write("<br />removed_item is: " + removed_item);


Output will be:-

After adding 1: orange,mango,pineapple,banana,sugar,tea
removed_item is:
After adding 1: orange,mango,pineapple,sugar,tea
removed_item is: banana

Comments or Responses

Login to post response