JavaScript Array Method : splice()

The splice() method is used to changes the content of an array, adding new elements while removing old elements. The basic syntax of splice method is as follows :
  array.splice(index, how_many_to_remove, [element1, element2, ..., element_n]);
where
  • The index represents from where to start add new elements.
  • The how_many_to_remove indicate the number of old elements to remove, if you don't want to remove any elemet then put 0.
  • The element1, element2, ..., element_n shows the list of elements to add.

Example :
<script>
  var webdev = ["HTML", "CSS", "Javascript", "MySql", "PHP"];
  document.write(webdev + "<br/>");
  webdev.splice(3, 0, "ASP.Net", "JSP");
  document.write("After adding 2 elements Array will be : " + webdev + "<br/>");
  var myvar = webdev.splice(5, 1);
  document.write("After removing 1 element : " + myvar + "<br/>"); 
  document.write("Array will be : " + webdev);
</script>
Output :


Next Topic :